fatfrog
fatfrog

Reputation: 2160

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?

Upvotes: 1

Views: 99

Answers (2)

Andy Waite
Andy Waite

Reputation: 11076

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}

Upvotes: 1

Doon
Doon

Reputation: 20232

does

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

do what you want?

Upvotes: 4

Related Questions