我正在使用rspec测试我的项目,现在我可以使用控制器部分了。我正在测试这种方法:
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
这是测试部分:
it "should accept an invite (1)" do
invite = Invite.create(:app => "test", :sender => "2", :receiver => "1")
get :accept, :id => 1
assert_response :success
end
但是当我运行rspec命令时出现此错误:
InvitesController should accept an invite (1)
Failure/Error: @dev = Developer.find(@invite.sender)
NoMethodError:
undefined method `sender' for nil:NilClass
所以我假设邀请对象是nil
,但我无法弄清楚为什么会这样。我通过浏览器测试功能,一切正常。这也是在不同的控制器方法中导致相同的错误,每次只是因为我的邀请对象是nil
。为什么会这样?
答案 0 :(得分:1)
解决:
app
,current_developer
)
it "should accept an invite (1)" do
invite = Invite.create(:app => @app.name, :sender => "2", :receiver => @developer.id)
get :accept, :id => @app.id
assert_response :redirect
end