kurenn / market_place_api

The API on Rails tutorial application
142 stars 70 forks source link

Listing: 6.5: Failure/Error: @user.destroy #71

Open rohitgoyal opened 8 years ago

rohitgoyal commented 8 years ago

According to 6.1.3 the test should be all green but I am getting this error.

Failures:

1) User#products association destroys the associated products on self destruct Failure/Error: @user.destroy NoMethodError: undefined method `name' for nil:NilClass

./spec/models/user_spec.rb:47:in`block (3 levels) in <top (required)>'

Finished in 0.66239 seconds 46 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:44 # User#products association destroys the associated products on self destruct

This is my user_spec.rb

require 'spec_helper'

describe User do
  before { @user = FactoryGirl.build(:user) }

  subject { @user }

  it { should respond_to(:email) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:auth_token) }

  it { should be_valid }

    it { should validate_presence_of(:email) }
    it { should validate_uniqueness_of(:email) }
    it { should validate_confirmation_of(:password) }
    it { should allow_value('example@domain.com').for(:email) }
  it { should validate_uniqueness_of(:auth_token)}

  it { should have_many(:products) }

  describe "#generate_authentication_token!" do
    it "generates a unique token" do
      Devise.stub(:friendly_token).and_return("auniquetoken123")
      @user.generate_authentication_token!
      expect(@user.auth_token).to eql "auniquetoken123"
    end

    it "generates another token when one already has been taken" do
      existing_user = FactoryGirl.create(:user, auth_token: "auniquetoken123")
      @user.generate_authentication_token!
      expect(@user.auth_token).not_to eql existing_user.auth_token
    end
  end

  describe "#products association" do

    before do
      @user.save
      3.times { FactoryGirl.create :product, user: @user }
    end

    it "destroys the associated products on self destruct" do
      products = @user.products
      @user.destroy
      products.each do |product|
        expect(Product.find(product)).to raise_error ActiveRecord::RecordNotFound
      end
    end
  end

end
kurenn commented 7 years ago

Maybe you need to update your test database with:

% rake db:test:prepare