psyho / bogus

Fake library for Ruby
Other
359 stars 14 forks source link

Stub new method on a class #36

Closed allenwyma closed 10 years ago

allenwyma commented 11 years ago

I'm trying to stub the new method on a class, but cannot figure out how to do that. All in all, I want to return an object that is stubbed, so I can check to see in my controller (Rails project) what the responses will be if it can or cannot be saved.

Here's what I have tried, but are no use:

fake(:company, as: :instance) { Company }
fake(:company_class, new:company, as: :class) { Company }
wrozka commented 11 years ago

If you want to stub on a constant, just stub it: stub(Company).new { company }

Your snipped doesn't work because fake(:company_class, new:company, as: :class) creates a fake of class, but it doesn't replace the constant. If your fake name matches class name, you don't have to pass the class in a block. fake(:company) does the same as fake(:company, as: :instance) { Company }

allenwyma commented 11 years ago

Hmm, I did try that, before, but was having an issue with something. When I stub the new method of Company and then try to stub the returning company's save method, then I get an error of SystemStackError: stack level too deep

wrozka commented 11 years ago

We'll take a look, sounds like a bug in bogus. Thanks for reporting.

indrekj commented 10 years ago

I have similar problem. E.g:

object_class = fake(Object, as: :class)
object_instance = fake(Object)

stub(object_class).new { object_instance }

object_class.new == object_instance # FALSE