RSpec: describe, context, feature, scenario?

落爺英雄遲暮 提交于 2019-11-30 10:15:11

问题


describe, context, feature, scenario: What is the difference(s) among the four and when do I use each one?


回答1:


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

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

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

feature and scenario are a part of Capybara, and not RSpec, and are meant to be used for acceptance tests. feature is equivalent to describe / context, and scenario equivalent to it / example.

If you're writing acceptance tests with Capybara, use the feature / scenario syntax, if not use describe / it syntax.




回答2:


This morning, while writing some specs, I was having the same question. Usually, I mainly use describe and don't particularly think about this, but this morning I was dealing with lots of cases and different specs for one module, so it had to be easily understandable for the next developer that will read those specs. So I asked Google about this, and I found this: describe vs. context in rspec, whose answer I find quite interesting :

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.

So, relying on this principle, you'd write a spec like this:

#
# 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

Not sure if this is a generally accepted rule but I find this approach clear and quite easy to grasp.




回答3:


Expanding on Pierre's excellent answer, according to the docs:

The feature and scenario DSL correspond to describe and it, respectively. These methods are simply aliases that allow feature specs to read more as customer and acceptance tests.

So for those familiar with the Mocha terms describe and it (which are better suited to describing the behaviour of a test from a user's perspective, hence Mocha primarily functioning as a front end testing framework), you could:

  • choose to always and only use describe and it or another pairing
  • choose to use it inside of a context block that requires multiple assertions/tests to be made in a specific app state

Going with the second option, you can still follow the intention of "...wrap[ping] a set of tests against one functionality under the same state".

Thus 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

This way you skip the feature keyword entirely, which you might want to use for specific front end features or if you are doing FDD (feature driven development), which may feel uncomfortable for some. Ask your developer team for input here.

Caveat: don't always follow industry standards, imagine if we modelled all our tests after the Volkswagen philosophy?



来源:https://stackoverflow.com/questions/11643747/rspec-describe-context-feature-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!