-
-
Save syed-shibli/05ec5153b31678e35788f352bd7feac9 to your computer and use it in GitHub Desktop.
describe vs. context in rspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe vs. context in rspec | |
http://lmws.net/describe-vs-context-in-rspec | |
In Rspec world, you often see people using both “describe” blocks and “context” blocks together, like this | |
describe "launch the rocket" do | |
context "all ready" do | |
end | |
context "not ready" do | |
end | |
end | |
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 | |
describe "launch the rocket" do | |
before(:each) do | |
#prepare a rocket for all of the tests | |
@rocket = Rocket.new | |
end | |
context "all ready" do | |
before(:each) do | |
#under the state of ready | |
@rocket.ready = true | |
end | |
it "launch the rocket" do | |
@rocket.launch().should be_true | |
end | |
end | |
context "not ready" do | |
before(:each) do | |
#under the state of NOT ready | |
@rocket.ready = false | |
end | |
it "does not launch the rocket" do | |
@rocket.launch().should be_false | |
end | |
end | |
end | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment