为什么这个简单的销毁方法期望不起作用?
在这里做了一些研究并使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您提供的内容,我想您应该简单地
在
expect
块之前添加: 。为什么?看起来你并没有真正使用 url 中传递的 id,而是存储在 session 中的值。但可以肯定的是,您应该提供
current_cart
方法。According to what you provide, I guess you should simply add:
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.在您的控制器中,您正在销毁
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 howcurrent_cart
is being created/used.