PaulWallis42
PaulWallis42

Reputation: 91

Can I mock the class name of an RSpec double?

I have an RSpec test file where I have created a double that allows a method and returns a value. In my Ruby application code I have used the code

if some_object.class == ClassName

to filter out unwanted instances of other classes.

The code works and gives the correct results but will not pass the RSpec test as the double gets filtered out during the test as it's classname is RSpec::Mocks::Double.

I have looked around but can't fin how to mock the class name of the actual double. Any ideas?

Upvotes: 2

Views: 1637

Answers (1)

Ilya
Ilya

Reputation: 13487

You can stub class invocation:

expect(some_object).to receive(:class).and_return(ClassName)

Upvotes: 3

Related Questions