How to mock a non-existent class with Rspec Mock

0

I am creating a lib that will use a model of Rails , but for the development of this lib I need to test without the presence of this model .

Then think of mockar (I'm using the gem rspec-mock ), so I tried:

it '#user_autenticated?' do
  allow('User').to receive(:where).and_return([double('User', cpf: '00011122233344')])

  auth = Rbot::Auth.new(maestro)
  expect(auth.user_autenticated?).not_to be nil
end

But unfortunately I get the error:

"User" does not implement: where

Does anyone know a way out of this problem?

    
asked by anonymous 20.03.2017 / 20:00

1 answer

0

I think you can do it in many ways, but for your case you need an ActiveRecord method, the best option would be mock_model .

Something like:

user = mock_model(User)
allow(user).to receive(:where).and_return([double('User', cpf: '00011122233344')])

Or if you're already using FactoryGirl

FactoryGirl.build_stubbed(:user)

This post contains more details: link

    
25.05.2017 / 16:26