test_should_destroy_product(ProductsControllerTest)问题

时间:2012-07-19 14:36:10

标签: ruby-on-rails ruby-on-rails-3 testing

我有以下内容:

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

2 个答案:

答案 0 :(得分:0)

我认为您需要在@product

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

assert_redirected_to products_path
end

答案 1 :(得分: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>