ruby on rails - Destroy method in testing controller with rspec -


i have transaction model, in have following scope :

scope :ownership, -> { property: true } 

i made tests of controller (thanks m. hartl). there :

require 'spec_helper'  describe transactionscontroller    let(:user) { factorygirl.create(:user) }   let(:product) { factorygirl.create(:givable_product) }    before { be_signed_in_as user }    describe "ownerships"      describe "creating ownership ajax"        "should increment ownership count"         expect           xhr :post, :create, transaction: { property: true, user_id: user.id, product_id: product.id }         end.to change(transaction.ownership, :count).by(1)       end        "should respond success"         xhr :post, :create, transaction: { property: true, user_id: user.id, product_id: product.id }         expect(response).to be_success       end     end      describe "destroying ownership ajax"       let(:ownership) { user.transactions.ownership.create(product_id: product.id, user_id: user.id) }        "should decrement ownership count"         expect           xhr :delete, :destroy, id: ownership.id         end.to change(transaction.ownership, :count).by(-1)       end        "should respond success"         xhr :delete, :destroy, id: ownership.id         expect(response).to be_success       end     end   end end 

and there destroy method of transaction controller :

def destroy   @transaction = transaction.find(params[:id])   @property = @transaction.property   @product = @transaction.product   @transaction.destroy   respond_to |format|     format.html { redirect_to @product }     format.js   end end       

but when run tests, 1 of them fails, , don't understand how or why :

1) transactionscontroller ownerships destroying ownership ajax should decrement ownership count    failure/error: expect      count should have been changed -1, changed 0    # ./spec/controllers/transactions_controller_spec.rb:31:in `block (4 levels) in <top (required)>' 

can me ?

you can use 'let!'.

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


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -