试图通过协会模仿“新”

时间:2009-10-22 22:48:19

标签: ruby-on-rails rspec

这是我的控制器

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

@businesses是一个business个对象的数组,我不确定如何模拟这个级联的调用。

1 个答案:

答案 0 :(得分:2)

这是一种方法。它的“业务”部分是一个关联代理。所以通常嘲笑它:

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

或更明确的

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)