[英]Destroy method in testing controller with rspec
I have a Transaction
model, in which I have the following scope : 我有一个
Transaction
模型,其中有以下范围:
scope :ownership, -> { where property: true }
I made some tests of the controller (thanks to M. Hartl). 我对控制器做了一些测试(感谢M. Hartl)。 There they are :
它们是:
require 'spec_helper'
describe TransactionsController do
let(:user) { FactoryGirl.create(:user) }
let(:product) { FactoryGirl.create(:givable_product) }
before { be_signed_in_as user }
describe "Ownerships" do
describe "creating an ownership with Ajax" do
it "should increment the Ownership count" do
expect do
xhr :post, :create, transaction: { property: true, user_id: user.id, product_id: product.id }
end.to change(Transaction.ownership, :count).by(1)
end
it "should respond with success" do
xhr :post, :create, transaction: { property: true, user_id: user.id, product_id: product.id }
expect(response).to be_success
end
end
describe "destroying an ownership with Ajax" do
let(:ownership) { user.transactions.ownership.create(product_id: product.id, user_id: user.id) }
it "should decrement the Ownership count" do
expect do
xhr :delete, :destroy, id: ownership.id
end.to change(Transaction.ownership, :count).by(-1)
end
it "should respond with success" do
xhr :delete, :destroy, id: ownership.id
expect(response).to be_success
end
end
end
end
And there is the destroy
method of my Transaction
controller : 还有我的
Transaction
控制器的destroy
方法:
def destroy
@transaction = Transaction.find(params[:id])
@property = @transaction.property
@product = @transaction.product
@transaction.destroy
respond_to do |format|
format.html { redirect_to @product }
format.js
end
end
But when I run the tests, one of them fails, and I don't understand how or why : 但是,当我运行测试时,其中之一失败了,我也不知道如何或为什么:
1) TransactionsController Ownerships destroying an ownership with Ajax should decrement the Ownership count
Failure/Error: expect do
count should have been changed by -1, but was changed by 0
# ./spec/controllers/transactions_controller_spec.rb:31:in `block (4 levels) in <top (required)>'
Can you help me about it ? 你能帮我吗?
You can use 'let!'. 您可以使用“ let!”。
About 'let' and 'let!': https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/helper-methods/let-and-let 关于“让”和“让!”: https : //www.relishapp.com/rspec/rspec-core/v/2-6/docs/helper-methods/let-and-let
From the RSpec documentation there's a difference between let
and let!
从RSpec文档中,
let
和let!
之间是有区别的let!
( see here ); ( 见这里 );
Use let to define a memoized helper method.
使用let定义一个记忆的辅助方法。 The value will be cached across multiple calls in the same example but not across examples.
该值将在同一示例中的多个调用之间缓存,但不会跨示例。
Note that let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked.
请注意,let是惰性求值的:直到第一次调用它定义的方法时,才进行求值。 You can use let!
您可以使用let! to force the method's invocation before each example.
强制在每个示例之前调用方法。
In your destroy method use let!(:ownership)
so that the ownership
object is not cached after it is destroyed. 在您的destroy方法中,使用
let!(:ownership)
以便在销毁ownership
对象后不对其进行缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.