tl; dr: go to the last paragraph
I recently tried to use RSpec query specifications for more targeted testing.
Here is what my testing looks like:
- the general specification of the characteristics of the cucumber , that is, the user goes to the post with a comment, upvotes to the comment and the author receives points
- model specifications , if the model really has some functionality, i.e.
User#upvote(comment) - controller specs where I block most things and just try to make sure the code goes as I expect
- look through the specifications if there is something complicated in the view, for example, rendering the upvote link only when the user has not already started, and they will also be hatched
The problem is that I have a specific script that is causing the error, and everything seems to work at the model / view level, where I cannot reproduce it.
It makes me write an integration test, which I can also do in a cucumber. The problem arises when I can really reproduce it, and I need to find out why this is happening. Usually this means playing tests, changing things, and seeing what happens.
For example, create a comment that belongs to the user who is trying to boost, try to vote with an expired session, etc. However, it is really a huge pain to write in Cucumber, because of the need to write a script, and then specify each step.
At this point, I prefer to write a query specification because it is lower and allows me to do something directly. The problem is that I'm not quite sure how to write the specification of the request correctly or what rules.
A simple example:
visit login_path fill_in "Username", :with => user.username fill_in "Password", :with => user.password click_button "Log in"
against
post sessions_path(:username => user.username, :password => user.password)
or even something lower, for example
session[:user_id] = user.id
Both of these examples achieve the same, they will register the user. I know that the answer to which to choose is based on what I need to check, but this does not answer the correct, traditional way for this.
I tried to find something about query specifications, but they are not described anywhere. The RSpec book does not apply to them; the RSpec documentation does not say anything.
What is the correct way to write query specifications? When should you use capybara and when only the Rails' #get and #post instead of clicking buttons and visit transition paths?