Why is my rspec test failing?

Posted by Justin Meltzer on Stack Overflow See other posts from Stack Overflow or by Justin Meltzer
Published on 2011-02-27T05:52:56Z Indexed on 2011/02/27 7:25 UTC
Read the original article Hit count: 145

Here's the test:

        describe "admin attribute" do

        before(:each) do
          @user = User.create!(@attr)
        end

        it "should respond to admin" do
          @user.should respond_to(:admin)
        end

        it "should not be an admin by default" do
          @user.should_not be_admin
        end

        it "should be convertible to an admin" do
          @user.toggle!(:admin)
          @user.should be_admin
        end
      end

Here's the error:

  1) User password encryption admin attribute should respond to admin
 Failure/Error: @user = User.create!(@attr)
 ActiveRecord::RecordInvalid:
   Validation failed: Email has already been taken
 # ./spec/models/user_spec.rb:128

I'm thinking the error might be somewhere in my data populator code:

require 'faker'

namespace :db do
  desc "Fill database with sample data"
  task :populate => :environment do
    Rake::Task['db:reset'].invoke
    admin = User.create!(:name => "Example User",
                 :email => "[email protected]",
                 :password => "foobar",
                 :password_confirmation => "foobar")
    admin.toggle!(:admin)             
    99.times do |n|
      name  = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password  = "password"
      User.create!(:name => name,
                   :email => email,
                   :password => password,
                   :password_confirmation => password)
    end
  end
end

Please let me know if I should reproduce any more of my code.

UPDATE: Here's where @attr is defined, at the top of the user_spec.rb file:

require 'spec_helper'

describe User do

  before(:each) do
      @attr = { 
        :name => "Example User", 
        :email => "[email protected]",
        :password => "foobar",
        :password_confirmation => "foobar" 
      }
    end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about error