私は次のものを持っています:
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
と私のテスト:
test "should destroy product" do
assert_difference('Product.count', -1) do
delete :destroy, :id => @product
end
assert_redirected_to products_path
end
そして私は得る:
# Running tests:
...............F......
Finished tests in 0.628844s, 34.9848 tests/s, 55.6577 assertions/s.
1) Failure:
test_should_destroy_product(ProductsControllerTest) [/Users/noahc/Dropbox/Projects/depot/test/functional/products_controller_test.rb:51]:
"Product.count" didn't change by -1.
<2> expected but was
<3>.
22 tests, 35 assertions, 1 failures, 0 errors, 0 skips
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [/usr/local/bin/ruby -I"lib:test" -I"/usr/l...]>
これが失敗する理由はありますか?
アップデート
コメントアウトした場合:
before_destroy :ensure_not_referenced_by_any_line_item
#ensure that there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
if line_items.any?
return true
else errors.add(:base, 'Line Items present')
return false
end
end
テストに合格します。ただし、@productには広告申込情報はありません。それは単なる備品です。