Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller Specs vs Request Specs?

I'm working on a rails API and I'm now planning on writing some RSpec tests for the controllers. I've been reading around and I haven't been able to figure out what the actual difference between controller specs and request specs are and which one I should probably use if I'm testing an API.

like image 979
jsanch16 Avatar asked Nov 28 '16 19:11

jsanch16


People also ask

What are controller specs?

Controller specs are RSpec wrappers for Rails functional tests. They simulate a single HTTP request in each example. By default views are not rendered; the controller spec stubs views with a template that renders an empty string (the template must exist).

How do I test a Rails controller?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.

What is describe in RSpec?

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.

What is assigns RSpec?

assigns relates to the instance variables created within a controller action (and assigned to the view).


1 Answers

Indeed, the RSpec team officially states controller specs are now obsolete.

http://rspec.info/blog/2016/07/rspec-3-5-has-been-released/

For new Rails apps: we don't recommend adding the rails-controller-testing gem to your application. The official recommendation of the Rails team and the RSpec core team is to write request specs instead. 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. In Rails 5, request specs are significantly faster than either request or controller specs were in rails 4, thanks to the work by Eileen Uchitelle1 of the Rails Committer Team.

like image 134
Quv Avatar answered Sep 23 '22 20:09

Quv