为什么这个简单的销毁方法期望不起作用?

发布于 2024-12-12 01:58:33 字数 1467 浏览 0 评论 0原文

在这里做了一些研究并使用 Google 后,我仍然很困惑为什么这个简单的规范不起作用:

describe CartsController do
  #stuff omitted...
  describe "carts#destroy" do
    it "destroys the requested cart" do
      cart = FactoryGirl.create(:cart)
      puts "Cart count = #{Cart.count}"
      expect {
        delete :destroy, :id => cart.id
      }.to change(Cart, :count).by(-1)
    end
  end
#stuff omitted...
end

这是 CartsController 的操作:

class CartsController < ApplicationController

  def destroy
    @cart = current_cart
    @cart.destroy
    session[:cart_id] = nil

    respond_to do |format|
      format.html { redirect_to(store_url, :notice => 'Your cart is currently empty') }
      format.json { head :ok }
    end
  end

end

最后但并非最不重要的是,我得到的错误

Cart count = 1
F

Failures:

  1) CartsController carts#destroy destroys the requested cart
     Failure/Error: expect {
       count should have been changed by -1, but was changed by 0
     # ./spec/controllers/carts_controller_spec.rb:146:in `block (3 levels) in <top (required)>'

Finished in 6.68 seconds
1 example, 1 failure

我是 rspec 测试的新手,但是,据我了解,我的销毁规范非常简单,并且应该按预期执行。我不知道我做错了什么。

请帮助我, 一如既往地感谢,

编辑..这是 current_cart 方法:

def current_cart
  Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
  cart = Cart.create
  session[:cart_id] = cart.id
  cart
end

After doing some research here, and using Google I'm still confused as to why this simple spec isn't working:

describe CartsController do
  #stuff omitted...
  describe "carts#destroy" do
    it "destroys the requested cart" do
      cart = FactoryGirl.create(:cart)
      puts "Cart count = #{Cart.count}"
      expect {
        delete :destroy, :id => cart.id
      }.to change(Cart, :count).by(-1)
    end
  end
#stuff omitted...
end

Here's the CartsController's action:

class CartsController < ApplicationController

  def destroy
    @cart = current_cart
    @cart.destroy
    session[:cart_id] = nil

    respond_to do |format|
      format.html { redirect_to(store_url, :notice => 'Your cart is currently empty') }
      format.json { head :ok }
    end
  end

end

And last but not least, the error I'm getting:

Cart count = 1
F

Failures:

  1) CartsController carts#destroy destroys the requested cart
     Failure/Error: expect {
       count should have been changed by -1, but was changed by 0
     # ./spec/controllers/carts_controller_spec.rb:146:in `block (3 levels) in <top (required)>'

Finished in 6.68 seconds
1 example, 1 failure

I'm new to rspec tests, however, As far as I understand my destroy spec is very straightforward, and it should perform as expected. I have no idea of what I'm doing wrong..

Please help me,
Thankful as always,

EDIT.. here's the current_cart method:

def current_cart
  Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
  cart = Cart.create
  session[:cart_id] = cart.id
  cart
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骑趴 2024-12-19 01:58:33

根据您提供的内容,我想您应该简单地

session[:cart_id] = cart.id

expect 块之前添加: 。

为什么?看起来你并没有真正使用 url 中传递的 id,而是存储在 session 中的值。但可以肯定的是,您应该提供 current_cart 方法。

According to what you provide, I guess you should simply add:

session[:cart_id] = cart.id

before your expect block.

Why? It seems you're not really using the id passed in the url but the value stored in session. But to be sure, you should provide your current_cart method.

决绝 2024-12-19 01:58:33

在您的控制器中,您正在销毁 current_cart ,它可能是在规范中的预期内创建的,然后被销毁,导致 0 的更改。检查 current_cart 是如何创建的/用过的。

In your controller you are destroying current_cart which might be getting created inside the expectation in the spec, and then destroyed, leading to a change of 0. Check how current_cart is being created/used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文