开发者

Trying to mock "new" through an association

T开发者_运维问答his 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.


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) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜