我的控制器能够创建一个子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)