简体   繁体   English

Rails Rspec测试控制器新动作

[英]Rails Rspec Testing Controller New Action

I am trying to test the new action in my controller. 我试图在我的控制器中测试新动作。 At the moment it looks like this: 目前它看起来像这样:

Controller 调节器

def new
  @business = Business.new
  @business.addresses.build
end

Spec 规格

describe 'GET #new' do
  it 'assigns a new business to @business' do
    get :new
    expect(assigns(:business)).to be_a_new(Business)

  end
end

I would like to test the line '@business.addresses.build'. 我想测试'@ business.addresses.build'这一行。 How do I do this? 我该怎么做呢?

Thanks in advance! 提前致谢!

怎么样

expect(assigns(:business).addresses.first).to be_a_new(Address)

Assuming build is a method, you only need test to ensure that build is called. 假设构建是一种方法,您只需要测试以确保调用构建。 You could do so by replacing the new Business with a mock that has an addresses attribute that expects to receive :build . 您可以通过使用具有期望接收的addresses属性的模拟替换新的Business来实现:build

I haven't tested this, but I suspect you could do something like: 我没有测试过这个,但我怀疑你可以这样做:

business = double('business')
addresses = double('addresses')
business.should_receive(:addresses).and_return(addresses)
addresses.should_receive(:build)
Business.stub(:new).and_return(business)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM