Inherited Resources and Rspec on Rails 3.1

September 29th, 2011

I'm trying to use Rspec to test a Rails 3.1 application that uses Inherited Resources, but I keep getting these types of errors with spec:views :

     Failure/Error: render
       undefined local variable or method `resource' for #<#:0xcb3bc88>

I believe it is in the way I am mocking objects:

  4   it "Renders payment form" do
  5     assign(:payment, stub_model(Payment))
  6     render
  7     view.should render_template(:partial => "_form", :count => 1)
  8   end

That would result in an instance variable @payment getting set in a mock controller, but that's not what my views are using anymore - they are using resource.

So what's a developer to do?

I'm under the impression that this before block will do the trick:

  before do
    controller.singleton_class.class_eval do
        def collection
        def resource
        helper_method :resource, :collection

Its far from perfect, but it does kind of work. I ended up with something slightly different than that, but that's the idea.

NOTE: Given that I'm using Factory there, it should be mentioned that I'm using factory_girl.

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022