ruby-on-rails – 在测试控制器时与rspec分配混合
发布时间:2020-12-17 03:04:43 所属栏目:百科 来源:网络整理
导读:我正试图掌握测试控制器的问题,到目前为止,我似乎仍然坚持最简单的问题. documents.controller: def edit @document = Document.find(params[:id]) end documents_controller_spec: describe 'GET #edit',focus: true do before(:each) { @doc = FactoryGi
我正试图掌握测试控制器的问题,到目前为止,我似乎仍然坚持最简单的问题.
documents.controller: def edit @document = Document.find(params[:id]) end documents_controller_spec: describe 'GET #edit',focus: true do before(:each) { @doc = FactoryGirl.create(:document)} it "should assign @document to the document" do get :edit,id: @doc assigns(:document).should eq(@doc) end end 始终返回false. @document总是分配为nil.我已经尝试将params [id]指定为@ doc.id,但这并没有解决任何问题.我在这做错了什么? 解决方法
从rspec的角度来看,您在行中比较的两个文档(分配给@doc):
assigns(:document).should eq(@doc) 不一样 – 它们在数据库中具有相同的ID,但是您比较的实际ruby对象是不同的,因为一个是在’before’块中创建的,而另一个可能是在GET请求执行时在控制器内实例化的在你的测试中. 我的猜测是以下工作: assigns(:document).id.should eq(@doc.id) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |