More on Bogus: Stubbing out all instances of a class

Posted by on December 13, 2013

I continue working with Bogus and finding how to do things that I think should be easy, but that Bogus doesn’t make very clear. Hopefully this blog post will help those similarly confused.

Today I needed to test a controller. The controller method is dead stupid.

def create
  account =
  if account.valid?
    render nothing: true
    head :bad_request

The normal Bogus approach relies heavily on dependency injection, which doesn’t work for controller tests. I can’t create the instance of Account outside of the controller and pass it into the create method. So I had to set up Bogus so that it would stub all instances of Account.

describe AccountsController do
  describe "Creation" do

    it "gives a bad request if the accout details are invalid " do
      stub(account).valid? {false}
      stub(Account).new(any_args) { account }

I don’t see this documented in Bogus itself, but I found the hint I needed by looking at this issue