kjs3
kjs3

Reputation: 6178

Trying to mock "new" through an association

This is in my controller

@business = @current_user.businesses.new(params[:business])

@businesses is an array of business objects and I'm unsure of how to mock this cascade of calls.

Upvotes: 0

Views: 420

Answers (1)

Karmen Blake
Karmen Blake

Reputation: 3436

Here is one way to do it. The 'businesses' part of it is an association proxy. So usually mock it like this:

business = Business.new
businesses_proxy = mock('business association proxy', :new => business)
@current_user.should_receive(:businesses).and_return(businesses_proxy)

or more explicit

business = Business.new
businesses_proxy = mock('business association proxy')
businesses_proxy.should_recieve(:new).and_return(business)
@current_user.should_receive(:businesses).and_return(businesses_proxy) 

Upvotes: 2

Related Questions