はい、それは可能ですが、あなたが使用したい構文(どこでもスペースを使用して)には、 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') }