我正在尝试通过将每个示例中实例化的内容放入基于此处文档的 let 方法来清理一些规范:https ://www.relishapp.com/rspec/rspec-core/v/2-13/docs /helper-methods/let-and-let!第二个规范中的 ObjectConnection 部分是作为 main_menu 的一部分创建的。如果我通过 Rails 控制台进行操作,它可以正常工作。
describe "shall comment on items" do
let(:menu) { FactoryGirl.create(:main_menu) } # i am assuming that I have access to a menu variable
# this one works
it 'should submit a message to a valid location' do
kt=menu.location
xhr :post, :create_message, {content: "here is my content", associated_global_id: kt.global_id }
response.code.should == "200"
end
# this one doesn't
it 'should submit a message to a valid object connection' do
pairing=ObjectConnection.first # multiple ObjectConnections are created as part of the main_menu factory
xhr :post, :create_message, {content: "here is my approval of your pairing seneor", associated_global_id: pairing.global_id } # this is where the error is
response.code.should == "200"
end
end
当我跑步时,我得到:
undefined method `global_id' for nil:NilClass
在第二个例子中
这应该如何工作?还是我需要在每个示例中声明 menu=FactoryGirl.create(:main_menu) ?