Why is this stubbed method that should return nil returning something else?

StackOverflow https://stackoverflow.com/questions/22160295

  •  19-10-2022
  •  | 
  •  

Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top