我可以通过糟糕的rspec设计进行此测试。我正在尝试将我的依赖项移到工厂,但它不起作用。我无法想办法让data_service
可以在测试中调用,并使用data_service_owner
工厂自动创建它。
这是spec文件:
describe Foo do
shared_context "notify" do
let!(:subscriber) { FactoryGirl.create(:subscriber) }
let!(:foo) { FactoryGirl.create(:foo) }
let!(:message) { FactoryGirl.create(:message) }
let!(:bar) { FactoryGirl.create(:bar) }
end
describe "#notify(subscriber)" do
include_context "notify"
it "notifies bars" do
bar.foo = foo #shouldn't have to do this
bar.save #shouldn't have to do this
expect { foo.notify_on_subscribe(subscriber) }.to change { Notification.all.count }.by(1)
end
end
我已经成功测试了所有的工厂。这里是data_service_owner
工厂:
factory :bar do
boolean_attr true
employee
foo #if I put FactoryGirl.create(:foo) here, it is not associated with the same foo object I defined in the let! statement
end
感谢。
答案 0 :(得分:0)
您应该能够通过执行类似
之类的操作将其关联到您的let块中let!(:bar) { FactoryGirl.create(:bar, foo: foo) }
并且应该将它与您在上面创建的foo相关联