How to mark a it_should_behave_like as pending

#automated-test, #dev, #ruby

tl;dr

Write something like:

context "something", pending: true do
  it_should_behave_like "an unauthorized controller" do
    before { post :execute, something: params }
  end
end

You read in my post How to create helper methods to RSpec I wrote a helper to avoid typing the same code again and again (DRY).

There are other ways in RSpec to be DRY. One of those is using an it_should_behave_like.

For instance, I have this code:

shared_examples_for "an unauthorized controller" do
  it "should return the correct error message" do
    expect(json_response["errors"][0]["message"]).to eq "Not authorized"
  end
end

And I call it:

it_should_behave_like "an unauthorized controller" do
  before { post :execute, something: params }
end

It is possible to have as many it as you want. In my case, I also check the code of the response.

However, in one of my controllers, it was not behaving like the shared example.

What should I do?

  • Turn it a pending example?
  • Comment those lines?
  • Remove the code?

Not calling it_should_behave_like does not seem right to me. The odds are that nobody will ever come back to add it.

Commenting those lines with a FIXME sounds like less bad, but something tells me that the odds of no one fixing it are also hight.

And two other reasons for using pending examples in RSpec are:

  • Alert that something needs to be fixed;
  • If the test comes back to pass RSpec will alert you about that.

I don’t see any problem running pending tests if we are going to receive a notification when this test becomes fixed.

The solution for this problem, in my case, was turning the context into a pending context, for my example:

context "when it is a regular API controller", pending: true do
  it_should_behave_like "an unauthorized controller" do
    before { post :execute, something: params }
  end
end

Don’t forget: In this case if the test passes your build will break.

References