我的控制器能夠創建一個子book_loan。我試圖在功能測試中測試這種行爲,但在使用assert_difference方法時遇到困難。我嘗試了很多方法將book_loans的計數傳遞給assert_difference,但沒有運氣。斷言在Ruby on Rails中關係中的子女數量的差異
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
不能轉換成BookLoan字符串
assert_difference(books(:ruby_book).book_loans,:count, 1)
NoMethodError:未定義的方法 'book_loans' 爲#
assert_difference('Book.book_loans.count', +1)
不能轉換成PROC字符串
assert_difference(lambda{books(:ruby_book).book_loans.count}, :call, 1)