Topic: ArgumentError: Sequence not registered: email

I keep getting this error when I run rspec: Sequence not registered: email.

However, I did set it in my factories.rb file. Any ideas on how to fix this? The app is running fine.

Thank you so much in advance!  ♥ ❤ ❥ ♥ ❤ ❥ ♥ ❤ ❥ ♥ ❤ ❥ ♥ ❤ ❥ ♥ ❤ ❥ ♥ ❤ ❥ ♥ ❤ ❥ ♥ ❤ ❥ ♥ ❤ ❥

Failures:

  1) UsersController GET 'index' for signed-in-users should be successful
     Failure/Error: @users << Factory(:user, :email => Factory.next(:email))
     ArgumentError:
       Sequence not registered: email
     # ./spec/controllers/users_controller_spec.rb:28:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/users_controller_spec.rb:27:in `times'
     # ./spec/controllers/users_controller_spec.rb:27:in `block (4 levels) in <top (required)>'

  2) UsersController GET 'index' for signed-in-users should have the right title
     Failure/Error: @users << Factory(:user, :email => Factory.next(:email))
     ArgumentError:
       Sequence not registered: email
     # ./spec/controllers/users_controller_spec.rb:28:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/users_controller_spec.rb:27:in `times'
     # ./spec/controllers/users_controller_spec.rb:27:in `block (4 levels) in <top (required)>'

  3) UsersController GET 'index' for signed-in-users should have an element for each user
     Failure/Error: @users << Factory(:user, :email => Factory.next(:email))
     ArgumentError:
       Sequence not registered: email
     # ./spec/controllers/users_controller_spec.rb:28:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/users_controller_spec.rb:27:in `times'
     # ./spec/controllers/users_controller_spec.rb:27:in `block (4 levels) in <top (required)>'

  4) UsersController GET 'index' for signed-in-users should paginate users
     Failure/Error: @users << Factory(:user, :email => Factory.next(:email))
     ArgumentError:
       Sequence not registered: email
     # ./spec/controllers/users_controller_spec.rb:28:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/users_controller_spec.rb:27:in `times'
     # ./spec/controllers/users_controller_spec.rb:27:in `block (4 levels) in <top (required)>'

Finished in 2.88 seconds
80 examples, 4 failures


factories.rb:

Factory.define :user do |user|
    user.name "Pavan Katepalli"
    user.email "email@pavan.com"
    user.password "foobar"
    user.password_confirmation "foobar"
end

Factory.sequence :email do |n|
  "person-#{n}@example.com"
end

users_controller_spec.rb:

require 'spec_helper'

describe UsersController do

    render_views

    describe "GET 'index'" do
        # book's way is on page 386 
        # I used the repo's way
        describe "for non-signed-in users" do
            it "should deny access" do
                get :index
                response.should redirect_to(signin_path)
                flash[:notice].should =~ /sign in/i
            end
        end

        describe "for signed-in-users" do

            before(:each) do
                @user = test_sign_in(Factory(:user))
                second = Factory(:user, :email => "another@example.com")
                third  = Factory(:user, :email => "another@example.net")

                @users = [@user, second, third]

                30.times do
                    @users << Factory(:user, :email => Factory.next(:email))
                end


            end

users_controller.rb:

class UsersController < ApplicationController
  before_filter :authenticate, :only => [:index, :edit, :update]
  before_filter :correct_user, :only => [:edit, :update]

  def index
    @title = "All users"
    @users = User.paginate(:page => params[:page])
  end

  def show
    @user = User.find(params[:id])
    @title = @user.name
  end

  def new
    @user = User.new
    @title = "Sign Up"
  end


  def create
    @user = User.new(params[:user])
    if @user.save
      # Handle a successful save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!" 
      redirect_to @user
    else
      @title = "Sign up"
      render 'new' 
    end
  end

  def edit
    @title = "Edit user"
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated."
      redirect_to @user
    else
      @title = "Edit user"
      render 'edit'
    end
  end

  private

    def authenticate
      deny_access unless signed_in?
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end
end

Last edited by cindysunshine1989 (2013-01-22 18:52:22)

Re: ArgumentError: Sequence not registered: email

Try to define your sequence as follows:

factory :user do
  sequence(:email, 1000) {|n| "person#{n}@example.com" }
end

Not sure the way you do it to be correct. See more about it here:
https://github.com/thoughtbot/factory_g … STARTED.md