Use contexts #3

andreareginato opened this Issue Oct 3, 2012 · 27 comments


None yet

Write your thoughts about the "use contexts" best practice.

andyw8 commented Oct 4, 2012

Deeply nested contexts can be confusing. I wouldn't use any more than 2 levels.


I agree with this best practice, but I feel there should also be example code of how to set up the context. The context method doesn't do anything by itself, just makes it more readable.

flov commented Oct 9, 2012

I agree with @andyw8 and disagree with the rule.
If there would be only one it block inside the context block, it reads much easier with using one it instead of using context and it.
It also requires you to setup the conditions in a before block which is another two lines just for before do and end

adarsh commented Oct 19, 2012

I disagree with using "should" in describe blocks.

Do or do not - there is no "should".

adarsh commented Oct 19, 2012

Edit: Wait - you already have this guideline here.

Why is it not used in this example (or others consistently)?

@tony612 tony612 added a commit to tony612/betterspecs that referenced this issue Mar 19, 2013
@tony612 tony612 finish #3 2e447d5
kikito commented May 1, 2013

This particular example confuses me.

it 'has 200 status code if logged in' do
  response.should respond_with 200


context 'when logged in' do
  it { should respond_with 200 }

My concern is that response has magically dissapeared. Where is it?


What's the best way to go about setting up the context? Should I just be using before? In the example, the context is magically set up.


It should be mentioned somewhere that you need shoulda included as gem for this matchers to actually work


@kikito response is the subject. Since this would be in a controller test, it gets assigned as the subject.


I agree that the comment by @balauru should be incorporated. Also the clarification by @warmwaffles would be nice too. These things may be obvious for experience Rails hackers but they aren't clear for a newbie.


@russellsilva essentially in the background this happens with controller tests:

subject { response }

If you wish to be more explicit, then by all means, use response

tubbo commented Sep 30, 2013

I really fail to see how one is any better than the other. You're adding extra syntax and work for the program just to save yourself a few characters? Also forgot to mention that shoulda isn't part of RSpec and therefore it {should.. doesn't work in straight RSpec. So now I have to load a whole 'nother gem just to save a few extra characters?

Most of these tips for writing more clear specs are appreciated, and make a lot of sense. But this one in particular struck me as a little snake oily.

siwka commented Oct 23, 2013

I am new to rspec. First I type a good solution and in result I have obvious for experienced users 'undefined method context'. Where is application of 'devoted to how to create a great RSpec test suite'? I wish to find reliable resource.


How are nested contexts supposed to be done?

context 'when something is set' do
  context 'and when another thing is set' do

is it proper to use and in the description?

tubbo commented Jan 9, 2014

I usually don't write "and" in my context descriptions, but if I could alias context "when #{message}" to when, I would :)- T

On Thu, Jan 9, 2014 at 1:15 PM, Matthew Johnston

How are nested contexts supposed to be done?

context 'when something is set' do
  context 'and when another thing is set' do

is it proper to use and in the description?

Reply to this email directly or view it on GitHub:
#3 (comment)


You can't use when with a lower case, however you can alias context to be When, however I don't think this is a good idea.


Yeah, I realized when was a reserved word almost immediately after I typed that. My bad.


The switch from inline matching to custom matchers from the shoulda gem in the before/after examples are extremely misleading for those of us new to RSpec.


Personally, I stay away from shoulda matchers. They are confusing and need to be avoided.


This is inconsistent with

It should be updated to follow the other guideline.


As a one liner syntax, I would rather recommend instead of should. While a little more verbose, it would keep the logic more consistent.

pedz commented Oct 5, 2014

For this particular topic (and perhaps others), I would like to see what rspec prints out. That would make me better able to evaluate the idea myself. Often I write specs and then rewrite them because the output when the test is run is useless.


Is it really okay to have nested context blocks?

onebree commented Jun 6, 2015

@xirukitepe I think it depends on your workflow and preference. At work, we use Capybara integration tests. Capybara DSL uses feature in place of context. Both work, but it just adds readibility to the spec. For us, a feature is a page (pretty much the controller used) in the app. I then create sub-contexts, like: context "SAD PATHS", because we do not use the it {should not} style. This way we can read the output and know if a test is for happy or sad paths

Do know, if you run random specs, they will be randomized as follows:

  1. By spec file
  2. By context (or feature or whatever DSL you use)
  3. By examples within the context

This means, if you have 2 contexts (one saying "Users page", and another saying "sad paths"), what will be randomized is:

  1. Spec file
  2. top level context
  3. second level context
  4. (and so on)
  5. examples
nagi commented Dec 7, 2015

How about amending:

When describing a context, start its description with "when" or "with"

... to ...

When describing a context, start its description with "when", "with", or "without"

Not so hot (in my opinion)

context 'with sprinkles' ...
context 'when there are no sprinkles on top' ...

Better (again, in my opinion)

context 'with sprinkles' ...
context 'without sprinkles' ...

🍦 🍨 🍧

onebree commented Dec 7, 2015

@nagi I like your idea. How about submitting a pull request with the change?

nagi commented Dec 7, 2015

@onebree Great, thanks! #157

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment