RSpec: describe, context, function, script?

describe , context , feature , scenario : What is the difference between the four and when do I use them?

+95
rspec rspec-rails rspec2
Jul 25 '12 at 6:15
source share
3 answers

context is an alias for describe , so they are functionally equivalent. You can use them interchangeably, the only difference is how your specification file reads. For example, there is no difference in test results. The RSpec book reads:

Msgstr "We tend to use describe() for things and context() for context."

Personally, I like to use describe , but I understand why people prefer context .

feature and scenario are part of Capybara, not RSpec, and are intended for use in acceptance tests. feature equivalent to describe / context , and scenario equivalent to it / example .

If you are writing acceptance tests with Capybara, use the feature / scenario syntax unless you use the describe / it syntax.

+127
Jul 26 2018-12-12T00:
source share

This morning, having written some specifications, I had the same question. Usually I mostly use describe and don't really think about it, but this morning I was dealing with a lot of cases and different specifications for one module, so for the next developer it should have been easy to understand to read these specifications, so I asked about it Google and I found the following: describe vs. context in rspec , whose answer I find quite interesting:

According to the rspec source code, “context” is simply a “describe” alias method, which means there are no functional differences between the two methods. However, there is a contextual difference that will help make your tests more understandable using both of them.

The goal of “describe” is to wrap a test suite for one functionality, and the “context” is to wrap a test suite for one functionality in the same state.

So, based on this principle, you should write such a specification:

 # # The feature/behaviour I'm currently testing # describe "item ordering" do # 1st state of the feature/behaviour I'm testing context "without a order param" do ... end # 2nd state of the feature/behaviour I'm testing context "with a given order column" do .. end # Last state of the feature/behaviour I'm testing context "with a given order column + reverse" do .. end end 

I’m not sure that this is a generally accepted rule, but I find this approach understandable and fairly easy to understand.

+30
Dec 03 '15 at 11:38
source share

I explain to Pierre the excellent answer , according to the documents :

The DSL specification and scenario matches the description and its correspondingly. These methods are simply aliases that allow function specifications to read more as user and acceptance tests.

Thus, for those who are familiar with Mocha's terms, this is also described (which are better suited to describe the behavior of the test from the point of view of the user, therefore, Mocha basically functions as a testing environment for the external interface), you could:

  • always choose and only use describe and it or another pairing
  • decided to use it inside the context block, which requires several statements / tests to be done in a certain state of the application

Moving on to the second option, you can still follow the intention of "... wrap the [ping] test suite for the same functionality in the same state."

So your tests might look like this:

 # # The feature/behaviour I'm currently testing # describe "item ordering" do # 1st state of the feature/behaviour I'm testing context "without an order param" do # 1st and only test we want to run in this state it "asks the user for missing order param" do ... end end # 2nd state of the feature/behaviour I'm testing context "with an invalid order param" do # 1st test we want to run in this state it "validates and rejects order param" do ... end # 2nd test we want to run in this state it "returns an error to user" do ... end end # 3rd state of the feature/behaviour I'm testing with multiple tests context "with a valid order param" do it "validates and accepts order param" do ... end it "displays correct price for order" do ... end unless being_audited it "secretly charges higher price to user" do ... end end end end 

Thus, you will miss the feature keyword as a whole, which you can use for specific interface functions or, if you do FDD (development, managed function), which may feel uncomfortable for some. Ask your development team for input here.

Caution: it does not always follow industry standards, imagine if we modeled all our tests in accordance with the philosophy of Volkswagen?

0
Jun 19 '19 at 11:24
source share



All Articles