[rspec-users] testing controller instance variables

Leo leogodin217 at gmail.com
Wed Jul 15 17:29:29 EDT 2009

HI All,
I have a controller with the following in it:

  def index
    @purchase_requests = PurchaseRequest.find(:all)
    @num_found = @purchase_requests.size

I want to test that @num_found is getting the number of purchase

This is what I've tried:

  it "should count the number of purchase requests found" do
    @purchase_requests.stub!(:size).and_return 3
    get :index

However, I Get This Error:

NoMethodError in 'PurchaseRequestsController Get index should count
the number of purchase requests found'
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.should_equal

It seems that rspec wants me to stub the instance variable.  If I do
that then I don't think I'd actually be testing anything because I'd
create the stub in the test and verify that the stub I created is
there.  I want to make sure that the test will fail if I delete the
line in the actual controller or change the name.  Can someone show me
how to do this?
thanks in advance.

More information about the rspec-users mailing list