ChatGPT解决这个技术问题 Extra ChatGPT

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

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.


A
Akshay Mohite

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.


So would you recommend using both feature and request specs, or is one or the other sufficient? (Being new to TDD...)
I use both, @robertwbradford. I use feature specs to drive my outside in tests - testing the user experience and then building out the functionality using unit tests. I use request specs for testing responses - e.g. in a sessions_spec I may have a describe "GET /login" block with expectations in it blocks such as expect(response.status).to eq(200) and expect(response).to render_template(:new), or in a describe "POST /sessions", a context "with valid credentials" block, with expect(response).to redirect_to(user) and follow_redirect!; expect(response.body).to include("Signed in")
And do you use controller specs too? It seems there's a bit of duplication between what you test in request specs and what is normally tested in controller specs.
Saying that, the above linked post clearly describes the differences. Use request specs to test via API, use feature specs to test via frontend.
@RichardJordan: A question: In feature specs, would you recommend to use Rails paths (i.e visit users_path) or hardcoded strings (visit '/users')?. Personally, I prefer not to use any app internals in those kind of specs.