RSpec: What is the difference between a feature and a request spec?

Ruby on-RailsRspecRspec Rails

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionharmView Question on Stackoverflow
Solution 1 - Ruby on-RailsRichard JordanView Answer on Stackoverflow