简体   繁体   English

Rspec-使用.attributes来让

[英]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. 当我这样做时,属性从section.attributes设置,但是complete: true被忽略。

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? 我还有另一种方法可以从base:section抓取属性,这样就不必每次都写所有属性了吗?

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: 一个常见的解决方案是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