是的,这是可能的,但是您要使用的语法(到处都有空格)的暗示是 have(:name)
和 eq('John')
所有参数都适用于该方法 should
. 。因此,您必须预先定义这些,这不是您的目标。也就是说,你可以使用 RSPEC自定义匹配器 实现类似目标:
require 'rspec/expectations'
RSpec::Matchers.define :have do |meth, expected|
match do |actual|
actual.send(meth) == expected
end
end
这为您提供了以下语法:
it { should have(:name, 'John') }
另外,您可以使用 its
its(:name){ should eq('John') }