开发者

Should I really test controllers?

开发者 https://www.devze.com 2023-03-08 14:46 出处:网络
I\'m trying to get the best codecoverage/development time result Currently I use rspec+shoulda开发者_JAVA技巧 to test my models and rspec+capybara to write my acceptance tests.

I'm trying to get the best codecoverage/development time result

Currently I use rspec+shoulda开发者_JAVA技巧 to test my models and rspec+capybara to write my acceptance tests.

I tried writing a controller test for a simple crud but it kinda took too long and I got a confusing test in the end(my bad probably)

What`s the best pratice on controller testing with rspec?

Here is a gist on my test and my controller(one test does not pass yet):

https://gist.github.com/991687 https://gist.github.com/991685


Maybe not.

Sure you can write tests for your controller. It might help write better controllers. But if the logic in your controllers is simple, as it should be, then your controller tests are not where the battle is won.

Personally I prefer well-tested models and a thorough set of integration (acceptance) tests over controller tests any time.

That said, if you have trouble writing tests for controllers, then by all means do test them. At least until you get the hang of it. Then decide whether you want to continue or not. Same goes for every kind of test: try it until you understand it, decide afterwards.


The way I view this is that acceptance tests (i.e. Cucumber / Capybara), test the interactions that a user would normally perform on the application. This usually includes things like can a user create a specific resource with valid data and then do they see errors if they enter invalid data. A controller test is more for things that a user shouldn't be able to normally do or extreme edge cases that would be too (cu)cumbersome to test with Cucumber.

Usually when people write controller tests, they are effectively testing the same thing. The only reason to test a controller's method in a controller test are for edge cases.

Edge cases such as if a user enters an invalid ID to a show page they should be shown a 404 page. This is a very simple kind of thing to test with a controller test, and I would recommend doing that. You want to make sure that when they hit the action that they receive a 404 response, boom, simple.

Making sure that your new action responds successfully and doesn't syntax error? Please. That's what your Cucumber features would tell you. If the action suddenly develops a Case of the Whoops, your feature will break and then you will fix that.

Another way of thinking about it is do you want to test a specific action responds in a certain way (i.e. controller tests), or do you care more about that a user can go to that new action and actually go through the whole motions of creating that resource (i.e. acceptance tests)?


Writing controller tests gives your application permission to lie to you. Some reasons:

  • controller tests are not executed in the environment they are run in. i.e. they are not at the end of a rack middleware stack, so things like users are not available when using devise (as a single, simple example). As Rails moves more to a rack based setup, more rack middlewares are used, and your environment deviates increasingly from the 'unit' behaviour.
  • You're not testing the behaviour of your application, you're testing the implementation. By mocking and stubbing your way through, you're re-implementing implementation in spec form. One easy way to tell if you're doing this; if you don't change the expected behaviour of url response, but do change the implementation of the controller (maybe even map to a different controller), do your tests break? If they do, you're testing implementation not behaviour. You're also setting your self up to be lied to. When you stub and mock, there's no assurances that the mocks or stubs you've setup do what you think they do, or even if the methods they're pretending to be exists after refactoring occurs.
  • Calling controller methods is impossible via your applications 'public' api. The only way to get to a controller is via the stack, and the route. If you can't break it from a request via a url, is it really broken?

I use my tests as an assurance the my application is not going to break when I deploy it. Controller tests add nothing to my confidence that my application is indeed functional, and actually their presence decreases my confidence.

One other example, when testing your 'behaviour' of your application, do you care that a particular file template was rendered, or that a certain exception was raised, or instead is the behaviour of your application to return some stuff to the client with a particular status code?

Testing controllers (or views) increases the burden of tests that you impose on yourself, and means that the cost of refactoring is higher than it needs to be because of the potential to break tests.


Should you test? yes

There are gems that make testing controllers faster

http://blog.carbonfive.com/2010/12/10/speedy-test-iterations-for-rails-3-with-spork-and-guard/


Definitely test the controller. A few painfully learned rules of thumb:

  • mock out model objects
  • stub model object methods that your controller action uses
  • sacrifice lots of chickens.


I like to have a test on every controller method at least just to eliminate stupid syntax errors that may cause the page to blow up.


A lot of people seem to be moving towards the approach of using Cucumber for integration testing in place of writing controller and routing tests.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号