RSpec: describe, context, feature, scenario?

29,783

Solution 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.

Solution 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.

Share:
29,783
Mike Glaz
Author by

Mike Glaz

Software Engineer with 4 years of experience building Angular/Rails apps seeking a mid-level backend or full stack engineer position. Familiar with current computer science methodologies such as object-oriented design and MVC architecture as well as an interest in the more modern reactive programming approach.

Updated on June 20, 2020

Comments

  • Mike Glaz
    Mike Glaz almost 4 years

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

  • rmcsharry
    rmcsharry about 8 years
    This is a very good answer for describe/context. But you forgot the other half of the question about feature/scenario - however I think they can (and should) be used in exactly the same way you point out for describe/context.
  • SpartaSixZero
    SpartaSixZero over 7 years
    Sam is on point and here is a link with greater detail and excellent examples, specifically on the section for Capybara's built in DSL(Domain Specific Language): github.com/jnicklas/capybara#using-capybara-with-rspec
  • GrayedFox
    GrayedFox almost 5 years
    Would be great if you expanded this with an explanation of feature/scenario!
  • Nikolay Moskvin
    Nikolay Moskvin almost 3 years
    Seems rubocop will not accept it by default rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Capybara/…