mercredi 20 juillet 2016

Getting nilClass error while testing with rspec

Hi i'm testing my project with rspec and now i'm up to the controllers part. I'm testing this method:

 def accept
@app = App.find(params[:id])
@invite = Invite.find_by(app: @app.name, receiver: current_dev.id)
@dev = Developer.find(@invite.sender)
@app.developers << @dev
respond_to do |format|
  if @invite.destroy
    format.html { redirect_to @app, notice: 'A new developer joined your team!' }
    format.json { render :show, status: :destroyed, location: @app }
  else
    format.html { render :back }
    format.json { render json: @invite.errors, status: :unprocessable_entity }
  end
end
end

and this is the test part:

    it "should accept an invite (1)" do

    invite = Invite.create(:app => "test", :sender => "2", :receiver => "1")
    get :accept, :id => 1
    assert_response :success

end

but when i run the rspec command i get this error:

   InvitesController should accept an invite (1)
 Failure/Error: @dev = Developer.find(@invite.sender)

 NoMethodError:
   undefined method `sender' for nil:NilClass

So i am assuming that the invite object is nil but i can't figure out why this happens. I test the function via browser and everything works fine. This is also causing same errors in different controller methods, everytime just because my invite object is nil. Why is this happening? Sorry for my english and for the post in general, very first time asking here.

Aucun commentaire:

Enregistrer un commentaire