Skip to content

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

@rohitgoyal

Description

@rohitgoyal

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions