Ruby-on-rails – RSpec: What is the difference between a feature and a request spec

rspecrspec-railsruby-on-rails

What is the conceptual difference between Rspec's feature specs and request specs?

From the feature spec docs:

Feature specs are high-level tests meant to exercise slices of functionality
through an application. They should drive the application only via its
external interface, usually web pages.

And for request specs:

Request specs provide a thin wrapper around Rails' integration tests, and are
designed to drive behavior through the full stack, including routing
(provided by Rails) and without stubbing (that's up to you).
With request specs, you can:

  • specify a single request
  • specify multiple requests across multiple controllers
  • specify multiple requests across multiple sessions

I know that feature specs use Capybara and request specs do not. But that hardly merits different concepts.

Best Answer

The conceptual difference is that you're usually testing a user story, and all interaction should be driven via the user interface. That's where Capybara comes in. A request spec is still testing the behavior of your application and doesn't have the expectation of readability that an acceptance test would have. So, feature is there for the improved syntax for acceptance tests.

Technical differences include request specs wrap Rails integration tests, whereas feature specs don't. This means with request specs you can use methods get, post, put, delete and assert against response. With feature specs you should drive all interaction through the browser and use methods like visit and assert against the page.

I'd recommend reading the feature_spec.feature in the rspec-rails source code on github. I hope this helps.

Related Topic