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 被忽略了。

是否有另一种方法可以从基础 :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}