describe vs. context in rspec

In Rspec world, you often see people using both “describe” blocks and “context” blocks together, like this

So what’s the difference between “describe” and “context” really?

According to the rspec source code, “context” is just a alias method of “describe”, meaning that there is no functional difference between these two methods. However, there is a contextual difference that’ll help to make your tests more understandable by using both of them.

The purpose of “describe” is to wrap a set of tests against one functionality while “context” is to wrap a set of tests against one functionality under the same state. Here’s an example

This code is more readable than wrapping all the stuffs with “describe” blocks. Because when you read the tests under “context”, you know they are all testing the same thing the “describe” talk about. And you know a “context” is to setup the state of your object, in this case @rocket.ready which will effects the behavior of @rocket.launch() that gives you a conclusion, right away, without scanning the code back and forth.

12 Replies to “describe vs. context in rspec”

  1. Yes, in case you use rspec as acceptance/integration tests framework.

    I think a more accurate definition of “individual scenarios” is the scenarios to test the same functionality under different states of the system.

  2. I was wondering how others were using context, so this is insightful, thanks.

    Another way of illustrating their relationship would be to use “describe” to wrap the user story and the “context” to wrap individual scenarios.

  3. You wrote “context” is just a alias method of “context”, but I think you meant ““context” is just a alias method of “describe””. Yes?

Leave a Reply