Question

I have an object named "Charity" and I want to make sure that each charity has a unique name, unfortunately I'm getting the following failed test, I suspect it's a problem with my factories:

Failures
     1) Charity name is already taken
       Failure/Error: it { @charity_with_same_name.should_not be_valid }
       NoMethodError:
         undefined method 'valid?' for nil:NilClass
       ...

The charity object belongs_to a user object. Here is the charity.rb:

class Charity < ActiveRecord::Base
  attr_accessible :name, :description, :summary
  belongs_to :user

  validates :name, presence: true, uniqueness: { case_sensitive: false }, length: { maximum: 40 }
  validates :summary, presence: true, length: { maximum: 140 }
  validates :description, presence: true
  validates :user_id, presence: true

  default_scope order: 'charities.created_at DESC'
end

Here are the relevant parts of the charity_spec.rb:

require 'spec_helper'

describe Charity do

  let(:user) { FactoryGirl.create(:user) }
  #before { @charity = user.charities.build(summary: "Lorem ipsum") }
  before { @charity = user.charities.build(FactoryGirl.attributes_for(:charity)) }
  # this makes sure that all the attributes for charity are set, whereas
  #the previous code only sets the "summary" attribute

  subject { @charity }
  ...
  describe "name is already taken" do
    before do
      charity_with_same_name = @charity.dup
      charity_with_same_name.name = @charity.name.upcase
      charity_with_same_name.save
    end

    it { @charity_with_same_name.should_not be_valid }
  end
  ...

Here is the factory.rb:

FactoryGirl.define do
  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com" }   
    password "foobar"
    password_confirmation "foobar"
    ...

    factory :charity do
      sequence(:name)        { |n| "charity #{n}" }
      sequence(:summary)     { |n| "summary #{n}" }
      sequence(:description) { |n| "description #{n}" }
    end
end

What did I do wrong?

Was it helpful?

Solution

You can make charity_with_same_name an instance variable in your before block (change charity_with_same_name to @charity_with_same_name), and change it { @charity_with_same_name.should_not be_valid } to specify { @charity_with_same_name.should_not be_valid } to get this to pass.

The issue is that @charity_with_same_name doesn't exist, because you haven't initialized it. You have only set up charity_with_same_name. Also, because of subject { @charity }, it refers to @charity, so it { @charity_with_same_name.should_not be_valid } doesn't make sense here.

EDIT: In ADDITION to doing the above, you'll need to add the line @charity.save in the before block (right before the line charity_with_same_name = @charity.dup). This is necessary because your duplicated record will pass the validation (when it shouldn't) despite the fact that it has the same name as @charity because @charity was not yet saved to the database. Once charity is saved, the validation test will check the database and see that the name has already been taken.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top