Pregunta

I have defined:

  • a view at views/pages/about.html.rb,
  • a test in spec/controllers/pages_controller_spec.rb:

    describe "GET 'about'" do
      it "should be successful" do
        get 'about'
        response.should be_success
      end
    end
    

I have not defined:

  • a corresponding action:

    class PagesController < ApplicationController
    
      # def about
      # end
    
    end
    
  • a route in routes.rb:

    Lily::Application.routes.draw do
      # get "pages/about"
    

I get an error in web browser but RSpec tests pass successfully as long as view is defined.
Is this expected behavior?


I'm using rspec-rails 2.0.1, webrat 0.7.1 and rails 3.2.1.
Scott found a similar issue closed by rspec-rails maintainers.

¿Fue útil?

Solución

This is the expected (perhaps somewhat surprising) behavior.

If you keep in mind how Rails separates concerns, it makes sense.
Controllers don't route—that happens in the dispatcher.

Try adding this in your test:

raise controller.params.inspect

Failures:
  1) PagesController GET pages/about
     Failure/Error: raise controller.params.inspect
     {"action"=>"about", "controller"=>"pages"}

get 'about' does not need to routed—the Rails testing framework takes care of that. Since the spec already knows what action it is supposed to handle, it goes ahead and calls PagesController#about.

The missing piece of the puzzle is that Rails doesn't need an action to be defined as long as the template exists; it will simply render about.html.erb.

So this test succeeds, as it should. When you call it live, it fails because there is no route. If you had written a request spec, routing spec, or a Cucumber test, it would also fail.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top