Question

I am stubbing a method like this:

User.stub_chain(:something).and_return(nil)

When I'm testing, I want this code to raise an error:

raise NameError if User.something.blank?

The problem is that User.something.blank? is not true, even though it should be stubbed with a nil value. User.something is actually

#[RSpec::Mocks::Mock:0x795359c @name=nil]

How do I fix this?

Was it helpful?

Solution

There's nothing wrong with your stub code or the code you're trying to test, as evidenced by the following passing test:

require 'spec_helper'

class User ; end
describe "" do
  it "" do
    User.stub_chain(:something).and_return(nil)
    expect { raise NameError if User.something.blank? }.to raise_error(NameError)
  end
end

You must have some problem elsewhere in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top