Transfer from Http://lmws.net/describe-vs-context-in-rspec
Learn RSpec, not quite understand describe and context. Google a bit, find this article, feel that there is some truth
1 in the RSpec world, we often see people using Descirbe code blocks and context code blocks such as
1 " launch the Rocket " Do 2 " all ready " Do 3 End 4 5 " not ready " Do 6 End7 End
So what's the difference between context and descript?
According to the RSpec source code, "context" was just a alias method of "describe", meaning that there was no functional di Fference between these, methods. However, there is a contextual difference that ' ll help to make your tests more understandable by using both of them.
said that, according to RSpec source, the context is only the alias of the describe method, meaning that there is no functional difference between the two, but there are some different contexts (different semantics), understand this can let you write more readable code
The purpose of "describe" is to wrap a set of tests against one functionality while "context" was to wrap a set of tests AG Ainst one functionality under the same state. Here's an example
The purpose of describe is when it wraps a group, a function, of the test code, while
The purpose of the context is to wrap a set of, in the same state, a functional, test code
For example, the following code
1Describe"launch the Rocket" Do2 before (: each) do3 #prepare a rocket for all of the tests4@rocket =rocket.new5 End6 7Context" all ready" Do8 before (: each) do9 #under the state of ReadyTen@rocket. Ready =true One End A -It"launch the Rocket" Do - @rocket. Launch (). Should be_true the End - End - -Context" not ready" Do + before (: each) do - #under the state of isn't ready +@rocket. Ready =false A End at -It"does not launch the rocket" Do - @rocket. Launch (). Should Be_false - End - End -End
The code above is good readability, not all things are used describe package, because you read the test code is in the context of the language, for a context set an object state,
@rocket. When ready is true, the behavior of the @rocket. Launch () results in a result that, if only one of these states is concerned, the rocket's startup results do not need to look at the other state of the code
RSpec describe and context are different