Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec what is the difference between be nil and be_nil

I am using rspec and for asserts like

student.name should be nil
student.name should be_nil

Both seem to work. is there a difference between using be nil an be_nil ???

like image 868
Abid Avatar asked Jun 01 '12 20:06

Abid


People also ask

What is expect in RSpec?

RSpec::Expectations provides a simple, readable API to express the expected outcomes in a code example. To express an expected outcome, wrap an object or block in expect , call to or to_not (aliased as not_to ) and pass it a matcher object: expect(order. total). to eq(Money. new(5.55, :USD)) expect(list).

How do I run a specific test in RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

What is subject in RSpec?

Summary: RSpec's subject is a special variable that refers to the object being tested. Expectations can be set on it implicitly, which supports one-line examples. It is clear to the reader in some idiomatic cases, but is otherwise hard to understand and should be avoided.

What is let in Ruby?

Use let to define a memoized helper method. The value will be cached across multiple calls in the same example but not across examples. Note that let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked.


1 Answers

There is no difference really, except be nil gets defined on the fly, and be_nil has been specifically programmed by rspec.

when you say should.be something, rspec tries the following

   [:==, :<, :<=, :>=, :>, :===].each do |operator|
      define_method operator do |operand|
        BeComparedTo.new(operand, operator)
      end
    end

Whereas, when you try should.be_nil it just checks

object.nil?

https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/matchers/built_in/be.rb

like image 169
DVG Avatar answered Sep 27 '22 21:09

DVG