Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'. How do I do this?

Thanks in advance!

like image 443
Rj01 Avatar asked Apr 30 '14 11:04

Rj01


2 Answers

How about

expect(assigns(:business).addresses.first).to be_a_new(Address)
like image 157
SteveTurczyn Avatar answered Sep 18 '22 12:09

SteveTurczyn


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.

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)
like image 24
maaachine Avatar answered Sep 21 '22 12:09

maaachine