Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a proper way of writing request specs in RSpec?

tl;dr: Jump to the last paragraph

Recently I've been trying to use RSpec's request specs to do some more targeted testing.

This is how my testing mostly looks:

  • general cucumber feature specification, i.e. user goes to a post with comment, upvotes on a comment and the author gets points
  • model specs for when the model actually has some functinality, i.e. User#upvote(comment)
  • controller specs where I stub most of the things and just try to make sure the code goes the way I expect
  • view specs for when there is something complex in the view, such as rendering a upvote link only when the user didn't already upvote, and these are stubbed as well

The problem is when I have some specific scenario which causes a bug and everything seems to work in the model/view layer where I am unable to reproduce it.

That forces me to write an integration test, which I can also do in cucumber. The problem arises once I am able to actually reproduce it, and I need to figure out why is it happening. This usually means playing around in tests, changing different things and seeing what happens.

For example create a comment that is owned by the user who is trying to upvote, try to vote with an expired session etc. However these are really huge pain to write in Cucumber, because of the need to write a scenario and then specify each step.

At this point, I prefer to write a request spec, because it is more low level and allows me to directly do stuff. The problem is, that I'm not really sure how to properly write a request spec, or what are the rules.

A simple example here is:

visit login_path
fill_in "Username", :with => user.username
fill_in "Password", :with => user.password
click_button "Log in"

vs

post sessions_path(:username => user.username, :password => user.password)

or even something more low level like

session[:user_id] = user.id # this actually doesn't work, but the idea is there

Both of these examples achieve the same thing, they'll log a user in. I know that the answer to which one to pick is based on what I need to test, but that doesn't answer the correct, conventional way to do this.

I've been trying to find something about request specs, but they're not really described anywhere. The RSpec book doesn't cover them, the RSpec documentation doesn't say anything either.

What is a correct way to write request specs? When should I use capybara and when just the Rails' #get and #post methods instead of clicking buttons and visiting paths?

like image 693
Jakub Arnold Avatar asked Jan 29 '12 17:01

Jakub Arnold


People also ask

What are request specs?

Request specs allow you to focus on a single controller action, but unlike controller tests involve the router, the middleware stack, and both rack requests and responses. This adds realism to the test that you are writing, and helps avoid many of the issues that are common in controller specs.

What is a spec in RSpec?

A controller spec is an RSpec wrapper for a Rails functional test. (ActionController::TestCase::Behavior). It allows you to simulate a single http request in each example, and then. specify expected outcomes such as: rendered templates.

What should I test in request specs?

Request specs give you the ability to test what your application does, rather than how it does it. For example, instead of testing that the right template is rendered in a controller spec, with a request spec we can test that the content we are expecting to appear actually appears in the response body.

What RSpec method is used to create an example?

The describe Keyword The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.


1 Answers

For requests spec I believe the convention is to stick to testing user behaviour and interface interactions, which would mean loading the page, filling in the form etc. A website user cant set the session or interact with variables directly so neither should your request specs.

I've often been tempted to skip page loads and form interactions by posting or setting variables in request specs (for speeds sake, especially heavy ajax specs) but it really does break the purpose of a request spec.

As the comments mentioned, you should test the specific controller / view behaviour in the other spec types.

like image 80
BigFive Avatar answered Nov 04 '22 17:11

BigFive