fearless_fool
fearless_fool

Reputation: 35189

verifying contents of a response: is that a controller test or an integration test?

Assume I have a list of named widgets. Somewhere in my test code, I want to verify that

widget = FactoryGirl.create(:widget)
get :index

generates a page that contains the string #{widget.name} somewhere on it.

My question: do I write this as a controller test or as an integration test?

(Aside #1: This seems outside the scope of a controller test, since it is assuming knowledge about what view is rendered. But writing it as an integration test feels too heavyweight, since it only requires a single request / response transaction.)

(Aside #2, my general question is "how do you decide what goes in controller tests and what goes in integration tests?" But this is arguably too broad for a Stack Overflow question.)

Upvotes: 1

Views: 69

Answers (2)

fearless_fool
fearless_fool

Reputation: 35189

Sometimes I think Stack Overflow should award an RTFM badge, and that I should be one of the first to receive it!

Digging around more, I found that RSpec supports "View specs" that are designed specifically to test the contents of what's rendered -- that's precisely what my original question was about.

The tests get written in spec/views/. Documentation and examples can be found here, but be sure to look at the documentation for your current version of RSpec.

Upvotes: 1

Chris Salzberg
Chris Salzberg

Reputation: 27374

I don't think there is any universally agreed-upon definition for these different categories of test, but personally I would call this a "request spec", a kind of integration test but one that involves only a single request/response cycle. It is clearly more than a controller test because it involves all elements of the MVC architecture. On the other hand, you're not clicking around and then finding buttons, clicking them etc. so it's not what I would typically think of as an "integration test".

One simple way to distinguish between what I would call "request specs" and what you typically would call "integration tests" is the use of get (from ActionDispatch) vs visit (from capybara). If you're only using get (and post etc.) then it is a request spec. If you're using visit then it's an integration test.

For more on the distinction between get and visit, see this post: http://blog.plataformatec.com.br/2012/06/improving-the-integration-between-capybara-and-rspec/ In that post there is a suggestion to get rid of spec/requests altogether and replace it by spec/api (for what I would call 'request specs') and spec/features (for what I would call 'integration tests').

Anyway note that what I've written above is just my own personal way of seeing this distinction, others may disagree.

Upvotes: 0

Related Questions