Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rohitgoyal opened this issue May 13, 2016 · 1 comment
Open

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

rohitgoyal opened this issue May 13, 2016 · 1 comment

Comments

@rohitgoyal
Copy link

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('[email protected]').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
Copy link
Owner

kurenn commented Feb 12, 2017

Maybe you need to update your test database with:

% rake db:test:prepare

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants