[rspec-users] Stumped with nil Class error

Ants Pants antsmailinglist at gmail.com
Fri Jan 7 12:58:52 EST 2011

I'm really, really confused about the use of instance variables in my
controller and their relationship to my controller spec.

I'm trying to spec a controller that has a before_filter :require_member
(it's Authlogic and set up in ApplicationController. It in turn calls
current_member - which  returns/creates the @member variable ) and a
before_filter :find_product

The find_product method is thus ...

def find_product
  @product = (params[:id]) ? @member.products.find(params[:id]) :

No matter what I try and in what order, I get the error message ....

NoMethodError in 'ProductsController PUT edit Product cut-off has passed
creates a product object'
undefined method `products' for nil:NilClass

and it points to the find_product method and it's the @member that is nil

I thought that the controller.stub(:require_member).and_return(member) would
suffice for the require_member stuff but I'm obviously wrong. I've even
replaced the member for @member in my spec but to no avail.

Here's the describe example ....

  describe "PUT edit" do
    context "Product cut-off has passed" do
      let(:member) { mock_model(Member).as_null_object }
      let(:product) { mock_model(Product).as_null_object }

      ## I just want it to create a @product object from
      it "creates a product object" do
        get :edit, :id => "1"

If anyone would be able to untangle me, I'd be greatly appreciative.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110107/8c6bc57d/attachment.html>

More information about the rspec-users mailing list