Filip Bartuzi
Filip Bartuzi

Reputation: 5931

Controller tests and stubbing filters/actions. What's the guideline?

Is stubbing filters/actions in controller specs considered as a good practise?

Let's say I have filter authorize! on almost every controller. Can I just stub it in all controller specs or I should have shared examples for testing each action if it has filter/action behaviour?

Things are getting messy when there are dozen of filters/actions on controllers (also inherited...)

Upvotes: 0

Views: 37

Answers (1)

zetetic
zetetic

Reputation: 47568

IMHO you should only stub when you need to. Some good reasons are:

  • The test depends on some well-documented external API that you don't control
  • The action/method to be stubbed is very slow and you have another test that covers it (i.e. it is a dependency of the test, not the subject of the test
  • You are building a new feature and stubbing code that does not yet exist, and you intend to remove the stub when the code is implemented

"Messiness" is not a good reason. RSpec provides a plethora of tools for cleaning up test code and making it more readable. Spend some time improving the tests and you may find that the desire for stubbing things out is reduced.

Upvotes: 2

Related Questions