[英]Rspec - using .attributes to dry up lets
我有兩個實例,它們之間的唯一區別是一個值(完整),我希望使用這樣的方法,但是它不起作用:
let(:section){Section.new(:date => '2015-05-01', :task_id => 1, :trade_id => 1, :schedule_id => 1)}
let(:complete_section){Section.new(section.attributes, complete: true)}
當我這樣做時,屬性從section.attributes
設置,但是complete: true
被忽略。
我還有另一種方法可以從base:section抓取屬性,這樣就不必每次都寫所有屬性了嗎?
確實
let(:complete_section){Section.new(section.attributes.merge(complete: true))}
你想做什么?
一個常見的解決方案是FactoryGirl ,您可以在其中定義模板並僅覆蓋必要的值:
let(:section) { FactoryGirl.build(:section) }
let(:complete_section) { FactoryGirl.build(:section, complete: true}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.