开发者

Trying to mock "new" through an association

开发者 https://www.devze.com 2022-12-09 11:34 出处:网络
T开发者_运维问答his is in my controller @business = @current_user.businesses.new(params[:business])

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

精彩评论

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