簡體   English   中英

Rspec-使用.attributes來讓

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM