Validate / Validate Rails with rspec Best Practices

For controller tests, Rails recommends checking things like HTTP response, authentication, destination, session, and flash messages. However, in the application I'm currently working with, I see a lot of Rspec tests with response.body.should have_tag (), which, as far as I can tell, are perfect for viewing tests.

I am wondering:

Is there any significant execution penalty / other type of punishment associated with such an imperfect testing method?

+6
source share
1 answer

If you view the views in your controller, this will result in your control tests taking longer. It all depends on whether you consider it appropriate to check for elements of the review (which, as a rule, makes your tests more fragile). If you do this, it will be easier to do this in the specifications of the controller, since you do not need to write out a separate view specification file.

If you want to test a lot of things in your views, you probably want to write separate specs of the form. Separating view specs, it is likely that the total time that your test suite will run will increase. However, for debugging purposes, it will be clear that something is wrong in the view against the controller with things disabled.

I suspect most Rails programmers don't write view specifications. Instead, they probably rely on their integration tests (Capybara +/- Cucumber) to test their views. However, integration tests take longer than unit tests. The RSpec book provides the following argument for writing individual view specifications:

Looking at the specifications gives us the opportunity to open the APIs that we need from controllers and models. This is not so valuable when the APIs comply with the most standard conventions. However, the value increases when we deviate from them ....

The only way to get an idea of ​​the benefits of them [viewing tests] is to learn to write them well. And only when you really understand how they fit into the flow, you can make informed decisions about when and when to use them.

+10
source

Source: https://habr.com/ru/post/890471/


All Articles