Rails Rspec Test Controller New Action

I am trying to test a 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 check the line '@ business.addresses.build'. How to do it?

Thanks in advance!

+4
source share
2 answers

What about

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

Assuming assembly is a method, you only need a test to ensure that the assembly is called. You can do this by replacing a new one with a Businesslayout that has an attribute addressesthat it expects to receive :build.

I have not 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)
+1
source

Source: https://habr.com/ru/post/1538723/


All Articles