mra*_*lau 3 ruby rspec matcher
有没有办法在rspec中匹配参数的属性值?检查的文件,它看起来像有其他可用的匹配,比如anything,an_instance_of和hash_including-但没有检查对象的属性值.
示例 - 假设我有此实例方法:
class DateParser
def parse_year(a_date)
puts a_date.year
end
end
Run Code Online (Sandbox Code Playgroud)
然后我可以这样写一个期望:
dp = DateParser.new
expect(dp).to receive(:parse_year).with(some_matcher)
Run Code Online (Sandbox Code Playgroud)
我希望some_matcher检查parse_year是否使用year具有值为2014 的属性的任何对象调用.这是否可以使用rspec中的开箱即用参数匹配,或者是否必须编写自定义参数?
Ste*_*fan 13
您可以传递一个块并设置对块内参数的期望:
describe DateParser do
it "expects a date with year 2014" do
expect(subject).to receive(:parse_year) do |date|
expect(date.year).to eq(2014)
end
subject.parse_year(Date.new(2014,1,1))
end
end
Run Code Online (Sandbox Code Playgroud)