我想通過將在每個示例中實例化的東西放入基於文檔的let方法來清除一些規格:https://www.relishapp.com/rspec/rspec-core/v/2-13/docs/helper-methods/let-and-let!第二個規範中的ObjectConnection部分是作爲main_menu的一部分創建的。如果我通過rails控制檯執行它,它工作正常。使用let和factorygirl兩個例子 - 不知道爲什麼這不起作用
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)?
啊......這就是我想要記住的。這也做了兩套插入,這不正是我正在尋找的,但它肯定解決了這個問題。謝謝 – timpone