[rspec-users] undefined local variable or method

Guirec Corbel guirec.corbel at gmail.com
Fri Jul 12 22:04:41 UTC 2013


Hello,

I retried with the version 2.14. I have this spec :

require 'spec_helper'

describe OrdersController do
   #...
   describe "POST 'create'" do
     subject(:do_post!) { post 'create', params }

     context "when the order and the credit card and the payment are 
valid" do
       before do
         allow(order).to receive(:valid?).and_return(true)
         allow(credit_card).to receive(:valid?).and_return(true)
         allow(payment).to receive(:valid?).and_return(true)
         do_post!
       end

       it { expect(order).to have_received(:save) }
     end
   end
end

And it works.

When I remove the "do_post!", in the context, I have this error :

      Failure/Error: it { expect(order).to have_received(:save) }
        (#<Order:0x007fcde323a308>).save(any args)
            expected: 1 time with any arguments
            received: 0 times with any arguments

Finally, I try to move the "do_post!" in the describe block and I have 
this :

       Failure/Error: subject(:do_post!) { post 'create', params }
        Double received unexpected message :valid? with (no args)
      # ./app/controllers/orders_controller.rb:6:in `create'
      # ./spec/controllers/orders_controller_spec.rb:28:in `block (3 
levels) in <top (required)>'
      # ./spec/controllers/orders_controller_spec.rb:29:in `block (3 
levels) in <top (required)>'


Should it working. I had similar spec with the previous versions and it 
was working.

Thanks!


More information about the rspec-users mailing list