RSpec describe and context are different

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.