我的控制器能够创建一个子 book_loan。我试图在功能测试中测试这种行为,但很难使用 assert_difference 方法。我尝试了多种将 book_loans 计数传递给 assert_difference 的方法,但都没有成功。
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 )