I have a model with an association which I am building in the controller for a nested form
def edit
@trans_op = TransportOperator.find params[:id]
@trans_op.coverages.build
end
How can I test that that the @trans_op.coverages.build line exists?
describe "GET edit" do
let!(:trans_op){ Factory :trans_op }
it "should be a success" do
get :edit, id: trans_op
response.should be_success
end
# this test needs to be fixed
it "should build an empty coverage if there are none" do
get :edit, id: trans_op
# ???
end
end
I’ve tried mocking:
it "should build an empty coverage if there are none" do
trans_op.coverages.should_receive(:build)
get :edit, id: trans_op
end
1) TransportOperatorsController GET edit should add an empty coverage if there are none
Failure/Error: Unable to find matching line from backtrace
([]).build(any args)
expected: 1 time
received: 0 times
and counting
it "should build an empty coverage if there are none" do
get :edit, id: trans_op
trans_op.coverages.count.should == 1
end
Failures:
1) TransportOperatorsController GET edit should add an empty coverage if there are none
Failure/Error: trans_op.coverages.count.should == 1
expected: 1
got: 0 (using ==)
You can access controller variables via
assignsin your specs. So for example this might do the trick:See http://rubydoc.info/gems/rspec-rails/2.8.1/file/README.md#assigns