简体   繁体   中英

Rspec - using .attributes to dry up lets

I have two instances, the only difference between them is one value (complete) I was hoping to use something like this but it doesn't work:

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)}

When I do that, the attributes get set from section.attributes , however complete: true is ignored.

Is there another way I can grab the attributes from the base :section so I don't need to write all the attributes every time?

does

let(:complete_section){Section.new(section.attributes.merge(complete: true))}

do what you want?

A common solution to this problem is FactoryGirl , where you can define a template and just override the necessary values:

let(:section) { FactoryGirl.build(:section) }
let(:complete_section) { FactoryGirl.build(:section, complete: true}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM