[rspec-users] rspec controller test strange error: undefined method `call' for nil:NilClass

Doug dgoldie15 at gmail.com
Sat Jul 3 02:20:27 EDT 2010


must be doing something dumb....
not sure, but the 'call' appears to be on the block.call when the
respond_to block gets executed.


straight forward "channels" resource with a 'create' action.... only
new thing is just assigning the current_user to the model.

  def create
    @channel = Channel.new(params[:channel])
    @channel.user = @user
    respond_to do |format|
      if @channel.save
        flash[:notice] = 'Channel was successfully created.'
        redirect_to(@channel)
      else
        flash[:error] = 'Error saving Channel.'
        render :action => "new"
      end
    end
  end

--------
modifying the scaffold spec like this
produces strange

  def mock_channel(stubs={})
    @mock_channel ||= mock_model(Channel, stubs)
  end
  def mock_user(stubs={})
    @mock_user ||= mock_model(User, stubs)
  end
  before(:each) do
    @mock_user = mock_user
    controller.stub(:current_user).and_return @mock_user
  end


  describe "POST create" do

    describe "with valid params" do
      it "assigns a newly created channel as @channel" do
        mock_channel(:save => true)
        @mock_channel.should_receive(:user=).with(@mock_user)
        Channel.stub(:new).with({'these' =>
'params'}).and_return(mock_channel)
        debugger
        post :create, :channel => {:these => 'params'}
        assigns[:channel].should equal(mock_channel)
      end
------
it fails on the post :create line ....reporting
undefined method `call' for nil:NilClass
at .../.rvm/gems/ruby-1.8.7-p249/gems/activesupport-2.3.5/lib/
active_support/whiny_nil.rb:52
       raise_nil_warning_for klass, method, caller
51      else
52        super
53      end
54    end

another clue perhaps is that if you remove the :save => true stub
option, then it works ...but fails at the
if @channel.save line     .....reporting
Mock "Channel_1002" received unexpected message :save with (no args)

thanks in advance


More information about the rspec-users mailing list