views:

221

answers:

1

My controller is able to create a child book_loan. I am trying to test this behavior in a functional test but am having a hard time using the assert_difference method. I've tried a number of ways of passing the count of book_loans to assert_difference with no luck.

  test "should create loan" do
    @request.env['HTTP_REFERER'] = 'http://test.com/sessions/new'
    assert_difference(books(:ruby_book).book_loans.count, 1) do
      post :loan, {:id => books(:ruby_book).to_param,
                               :book_loan => {:person_id => 1,
                                              :book_id =>
                                                books(:dreaming_book).id}}

    end
  end

can't convert BookLoan into String

assert_difference(books(:ruby_book).book_loans,:count, 1)

NoMethodError: undefined method 'book_loans' for #

assert_difference('Book.book_loans.count', +1)

can't convert Proc into String

assert_difference( lambda{books(:ruby_book).book_loans.count}, :call, 1 )
+2  A: 

It looks like assert_difference expects a string, which it will eval before and after the block. So the following may work for you:

assert_difference('books(:ruby_book).book_loans.count', 1) do
  ...
end
Greg Campbell