我有以下内容:
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 没有订单项。它只是一个固定装置。