2

I have a Transaction model, in which I have the following scope :

scope :ownership, -> { where property: true }

I made some tests of the controller (thanks to 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 :

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 ?

1
  • 1
    Old question, but just to note for the other users. If this error happens, then it is due to using let over let! (it should have been let!(:ownership) as it saves in the database, and the :count would make sense), and the second reason might be that the logic in the controller is failing (which this test should be testing in any-case :) ) Commented Apr 23, 2015 at 10:11

2 Answers 2

1

You can use 'let!'.

About 'let' and 'let!': https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/helper-methods/let-and-let

Sign up to request clarification or add additional context in comments.

Comments

0

From the RSpec documentation there's a difference between let and let! (see here);

Use let to define a memoized helper method. 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. You can use 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.