我有一个Rspec的测试使用FactoryBot(FactoryGirl)如下:如何在嵌套上下文中使用`let`和`create`时不重复编写相同的属性?
describe Note do
let(:note) {create(:note, title: "my test title", body: "this is the body")}
expect(note.title).to eq "my test title"
expect(note.body).to eq "this is the body"
context "with many authors" do
let(:note) {create(:note, :many_authors, title: "my test title", body: "this is the body")}
it "has same title and body and many authors" do
expect(note.title).to eq "my test title"
expect(note.body).to eq "this is the body"
expect(note.authors.size).to eq 3
end
end
end
在该试验中我有初始:note
与标题和主体。在嵌套的上下文中,我想使用相同的note
,但只需添加我的:many_authors
特征。但是,我发现自己不得不复制和粘贴前一个注释中的属性title: "my test title", body: "this is the body"
,所以我想知道干掉代码的最佳方法是什么,所以我不必总是复制和粘贴标题和主体属性。什么是正确的方法来做到这一点?