私はrspecを使用しており、次のようなアサートに使用しています
student.name should be nil
student.name should be_nil
どちらも機能しているようです。???の使用be nilに違いはありますかbe_nil
私はrspecを使用しており、次のようなアサートに使用しています
student.name should be nil
student.name should be_nil
どちらも機能しているようです。???の使用be nilに違いはありますかbe_nil
be nilオンザフライで定義され、be_nilrspec によって特別にプログラムされていることを除いて、実際には違いはありません。
あなたが言うとshould.be something、rspecは次のことを試みます
[:==, :<, :<=, :>=, :>, :===].each do |operator|
define_method operator do |operand|
BeComparedTo.new(operand, operator)
end
end
一方、試してみるshould.be_nilと、チェックするだけです
object.nil?
https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/matchers/built_in/be.rb
違いはないと思いますが、be_trueやなどの他のメソッドとの一貫性のために使用されbe_falseます。
内部では、両方の要素をbeチェックします。id
で動作しますnil
trueRubyではすべてがそうではfalseないため、失敗しますniltrue
false両方nilとfalse一致するので失敗します