Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure exactly what you want to test, and I think there's some confusion as to what methods can be used where, so I'll try and give examples of <a href="https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/routing-specs">Routing specs</a>, <a href="https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/request-specs/request-spec">Request Specs</a>, <a href="https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/controller-specs">Controller specs</a>, and <a href="https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/feature-specs/feature-spec">Feature specs</a>, and hopefully one of them will be appropriate for you.</p> <h3>Routing</h3> <p>If you want to make sure that your root path gets routed to the <code>home#index</code> action, a routing spec may be appropriate:</p> <p><strong>spec/routing/routing_spec.rb</strong></p> <pre><code>describe "Routing" do it "routes / to home#index" do expect(get("/")).to route_to("home#index") end end </code></pre> <h3>Request</h3> <p>If you want to make sure that the <code>index</code> template gets rendered on a request to your root path, a request spec may be appropriate:</p> <p><strong>spec/requests/home_requests_spec.rb</strong></p> <pre><code>describe "Home requests" do it 'successfully renders the index template on GET /' do get "/" expect(response).to be_successful expect(response).to render_template(:index) end end </code></pre> <h3>Controller</h3> <p>If you want to make sure that the <code>index</code> template gets rendered on a request to the <code>index</code> action of your <code>HomeController</code>, a controller spec may be appropriate (and quite similar to a request spec in this case, but focused exclusively on the controller):</p> <p><strong>spec/controllers/home_controller_spec.rb</strong></p> <pre><code>describe HomeController do describe "GET index" do it "successfully renders the index template" do expect(controller).to receive(:index) # this line probably of dubious value get :index expect(response).to be_successful expect(response).to render_template(:index) end end end </code></pre> <h3>Feature</h3> <p>If you want to make sure the page rendered by <code>home#index</code> has some specific content, a feature spec may be appropriate (and also the only place you can use <a href="http://rubydoc.info/github/jnicklas/capybara/master">Capybara methods</a> like <code>visit</code>, depending on your Rails/RSpec version):</p> <p><strong>spec/features/home_features_spec.rb</strong></p> <pre><code>feature "Index page" do scenario "viewing the index page" do visit root_path expect(page).to have_text("Welcome to my awesome index page!") end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload