0

我有以下内容:

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 没有订单项。它只是一个固定装置。

4

2 回答 2

0

我认为您需要添加to_param@product

test "should destroy product" do
assert_difference('Product.count', -1) do
  delete :destroy, :id => @product.to_param
end

assert_redirected_to products_path
end
于 2012-07-19T15:16:29.990 回答
0

我会考虑这一点,因为您可能不想评论防止产品在某人的订单历史记录中被删除的回调(对于未来的购买或当前购买,我将实施一个活动/非活动状态列,该列将被评估在最终结账之前;仍然允许查看产品,但阻止结账,例如在产品缺货的情况下)。这些测试将 product.rb 中的 before_destroy 考虑在内。在这里,我正在即时模拟如果从未购买过产品会是什么样子。或者,您可以尝试更新您的灯具。

test "product has line items" do
  assert_not_equal 0, @product.line_items.count
end

test "product has no line items" do
  @product.line_items.each do |item|
    item.destroy
  end

  assert_equal 0, @product.line_items.count
end

test "should destroy product" do
    assert_difference('Product.count', -1) do

    # "simulate" a product that has never been added to cart
    # alternatively you could update your fixtures to do this

    @product.line_items.each do |item|
      item.destroy
    end

    delete :destroy, id: @product
  end

  assert_redirected_to products_path
end

最后,请务必更新您的控制器和视图以添加此 Flash 通知,以便您查看发生了什么:

# DELETE /products/1
# DELETE /products/1.json
def destroy
  @product = Product.find(params[:id])
  if @product.destroy
    flash[:notice] = "#{@product.title} successfully deleted"
  else
    flash[:notice] = "It appears there are other carts that currently have #{@product.title} so we won't delete it at this time"
  end

  respond_to do |format|
    format.html { redirect_to products_url }
    format.json { head :no_content }
  end
end

在您对 Destroy 链接所在的产品的视图中,如果您决定使用此链接,请确保将通知打印在顶部:

<p id="notice"><%= notice %></p>
于 2013-03-23T07:25:54.680 回答