私は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_nil
rspec によって特別にプログラムされていることを除いて、実際には違いはありません。
あなたが言うと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
true
Rubyではすべてがそうではfalse
ないため、失敗しますnil
true
false
両方nil
とfalse
一致するので失敗します