Ruby on rails ArgumentError:序列未注册:电子邮件

Ruby on rails ArgumentError:序列未注册:电子邮件,ruby-on-rails,sequence,factory-bot,Ruby On Rails,Sequence,Factory Bot,当我运行rspec:Sequence not registed:email时,我一直收到这个错误 但是,我确实在factories.rb文件中设置了它。有没有办法解决这个问题?应用程序运行良好 Failures: 1) UsersController GET 'index' for signed-in-users should be successful Failure/Error: @users << Factory(:user, :email => Fact

当我运行rspec:Sequence not registed:email时,我一直收到这个错误

但是,我确实在factories.rb文件中设置了它。有没有办法解决这个问题?应用程序运行良好

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
用户\控制器\规范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

            it "should be successful" do
                get :index
                response.should be_success
            end

            it "should have the right title" do
                get :index
                response.should have_selector('title', :content => "All users")
            end

            it "should have an element for each user" do
                get :index
                @users[0..2].each do |user|
                #User.paginate(:page => 1).each do |user|
                    response.should have_selector('li', :content => user.name)
                end
            end

            it "should paginate users" do
                get :index
                response.should have_selector('div.pagination')
                response.should have_selector('span.disabled', :content => "Previous")
                #response.should have_selector('a', :href => "/users?page=2",
                #                                   :content => "2")
                #response.should have_selector('a', :href => "/users?page=2",
                #                                   :content => "Next")
            end
=begin
            it "should have delete links for admins" do
                @user.toggle!(:admin)
                other_user = User.all.second
                get :index
                response.should have_selector('a', :href => user_path(other_user),
                                                   :content => "delete")
            end

            it "should not have delete links for non-admins" do
                other_user = User.all.second
                get :index
                response.should_not have_selector('a', :href => user_path(other_user),
                                                       :content => "delete")
            end
=end
        end
    end

    describe "GET 'show'" do

        before(:each) do
          # used to be just Factory.build(:user)
          @user = Factory(:user)
        end

        it "should be successful" do
          get :show, :id => @user.id
          response.should be_success
        end

        it "should find the right user" do
          get :show, :id => @user.id

          # assigns(:user) returns the 
          # value of the instance variable @user
          assigns(:user).should == @user
        end

        it "should have the right title" do
          get :show, :id => @user.id
          response.should have_selector('title', :content => @user.name)
        end

        it "should have the user's name" do
          get :show, :id => @user.id
          response.should have_selector('h1', :content => @user.name)
        end

        it "should have a profile image" do
          get :show, :id => @user.id
          response.should have_selector('h1>img', :class => "gravatar")
        end
    end

    describe "GET 'new'" do

        it "should be successful" do
          get :new
          response.should be_success
        end

        it "should have the right title" do
          get :new
          response.should have_selector('title', :content => "Sign Up")
        end
    end

    describe "POST 'create'" do

        describe "failure" do

            before(:each) do
                @attr = { :name => "", :email => "", :password => "",
                      :password_confirmation => "" }
            end

            it "should not create a user" do
                lambda do
                    post :create, :user => @attr
                end.should_not change(User, :count)
            end

            it "should have the right title" do
                post :create, :user => @attr
                response.should have_selector('title', :content => "Sign up")
            end

            it "should render the 'new' page" do
                post :create, :user => @attr
                response.should render_template('new')
            end
        end

        describe "success" do

            before(:each) do
                @attr = { :name => "New User", :email => "user@example.com",
                      :password => "foobar", :password_confirmation => "foobar" }
            end

            it "should create a user" do
                lambda do
                    post :create, :user => @attr
                end.should change(User, :count).by(1)
            end

            it "should redirect to the user show page" do
                post :create, :user => @attr
                response.should redirect_to(user_path(assigns(:user)))
            end

            it "should have a welcome message" do
                post :create, :user => @attr
                flash[:success].should =~ /welcome to the sample app/i
            end

            it "should sign the user in" do
                post :create, :user => @attr
                controller.should be_signed_in
            end

        end
    end

    describe "GET 'edit'" do

        before(:each) do
            @user = Factory(:user)
            test_sign_in(@user)
        end

        it "should be successful" do
            get :edit, :id => @user
            response.should be_success
        end

        it "should have the right title" do
            get :edit, :id => @user
            response.should have_selector('title', :content => "Edit user")
        end

        it "should have a link to change the Gravatar" do
            get :edit, :id => @user
            gravatar_url = "http://gravatar.com/emails"
            response.should have_selector('a', :href => 'http://gravatar.com/emails',
                                             :content => "change")
        end
    end



    describe "PUT 'update'" do

        before(:each) do
            @user = Factory(:user)
            test_sign_in(@user)
        end

        describe "failure" do

            before(:each) do
                @attr = { :email => "", :name => "", :password => "",
                          :password_confirmation => "" }
            end

            it "should render the 'edit' page" do
                put :update, :id => @user, :user => @attr
                response.should render_template('edit')
            end

            it "should have the right title" do
                put :update, :id => @user, :user => @attr
                response.should have_selector('title', :content => "Edit user")
            end
        end

        describe "success" do

            before(:each) do
                @attr = { :name => "New Name", :email => "user@example.org",
                          :password => "barbaz", :password_confirmation => "barbaz" }
            end

            it "should change the user's attributes" do
                put :update, :id => @user, :user => @attr
                @user.reload
                @user.name.should  == @attr[:name]
                @user.email.should == @attr[:email]
                #@user.encrypted_password.should == assigns(:user).encrypted_password
            end

            it "should redirect to the user show page" do
                put :update, :id => @user, :user => @attr
                response.should redirect_to(user_path(@user))
            end

            it "should have a flash message" do
                put :update, :id => @user, :user => @attr
                flash[:success].should =~ /updated/
            end
        end
    end

    describe "authentication of edit/update actions" do

        before(:each) do
            @user = Factory(:user)
        end

        describe "for non-signed-in users" do

            it "should deny access to 'edit'" do
                get :edit, :id => @user
                response.should redirect_to(signin_path)
                #flash[:notice].should =~ /sign in/i
            end

            it "should deny access to 'update'" do
                put :update, :id => @user, :user => {}
                response.should redirect_to(signin_path)
            end
        end

        describe "for signed-in users" do

            before(:each) do
                wrong_user = Factory(:user, :email => "user@example.net")
                test_sign_in(wrong_user)
            end

            it "should require matching users for 'edit'" do
                get :edit, :id => @user
                response.should redirect_to(root_path)
            end

            it "should require matching users for 'update'" do
                put :update, :id => @user, :user => {}
                response.should redirect_to(root_path)
            end
        end
    end
end
require'spec\u helper'
描述UsersController做什么
渲染视图
描述“获取‘索引’”的操作
#书的路线在386页
#我用回购的方式
描述“针对未登录用户”的做法
它“应该拒绝访问”吗
获取:索引
response.should_重定向到(登录路径)
flash[:注意].should=~/sign-in/i
结束
结束
描述“针对已登录用户”的做法
在…之前做
@用户=测试登录(工厂(:用户))
第二个=工厂(:用户,:电子邮件=>)another@example.com")
第三个=工厂(:用户,:电子邮件=>)another@example.net")
@users=[@user,second,third]
30.5倍
@用户工厂。下一步(:电子邮件))
结束
结束
它“应该成功”吗
获取:索引
回答:你应该成功吗
结束
它“应该有正确的标题”吗
获取:索引
response.com应该有_选择器('title',:content=>“所有用户”)
结束
它“应该为每个用户提供一个元素”do
获取:索引
@用户[0..2]。每个do |用户|
#User.paginate(:page=>1)。每个do | User|
response.should有_选择器('li',:content=>user.name)
结束
结束
它“应该为用户分页”吗
获取:索引
response.should_选择器('div.pagination')
response.should有_选择器('span.disabled',:content=>“Previous”)
#response.com应该有_选择器('a',:href=>“/users?page=2”,
#:content=>“2”)
#response.com应该有_选择器('a',:href=>“/users?page=2”,
#:content=>“下一步”)
结束
开始
它“应该有删除管理员链接”吗
@user.toggle!(:admin)
其他用户=user.all.second
获取:索引
response.should有_选择器('a',:href=>user_路径(other_user),
:content=>“delete”)
结束
它“不应该有非管理员的删除链接”吗
其他用户=user.all.second
获取:索引
response.should_没有_选择器('a',:href=>user_路径(其他_用户),
:content=>“delete”)
结束
=结束
结束
结束
描述“得到‘表演’”做什么
在…之前做
#以前只是Factory.build(:user)
@用户=工厂(:用户)
结束
它“应该成功”吗
get:show,:id=>@user.id
回答:你应该成功吗
结束
它“应该找到正确的用户”做什么
get:show,:id=>@user.id
#assigns(:user)返回
#实例变量@user的值
分配(:用户)。应==@user
结束
它“应该有正确的标题”吗
get:show,:id=>@user.id
response.should有_选择器('title',:content=>@user.name)
结束
它“应该有用户名”吗
get:show,:id=>@user.id
response.should应具有_选择器('h1',:content=>@user.name)
结束
它“应该有一个配置文件图像”吗
get:show,:id=>@user.id
response.should有_选择器('h1>img',:class=>“gravatar”)
结束
结束
描述“获得‘新’”做什么
它“应该成功”吗
获取:新
回答:你应该成功吗
结束
它“应该有正确的标题”吗
获取:新
response.should有_选择器('title',:content=>“注册”)
结束
结束
描述“发布‘创建’”“做什么
描述“失败”是什么
在…之前做
@attr={:name=>“”,:email=>“”,:password=>“”,
:密码\u确认=>“”
结束
它“不应该创建用户”吗
lambda do
post:create,:user=>@attr
end.should\u不应更改(用户,:计数)
结束
它“应该有正确的标题”吗
post:create,:user=>@attr
response.should有_选择器('title',:content=>“注册”)
结束
它“应该呈现‘新’页面”吗
post:create,:user=>@attr
response.should呈现_模板('new')
结束
结束
描述“成功”是什么
在…之前做
@attr={:name=>“新用户”,“电子邮件=>”user@example.com",
:password=>“foobar”,:password\u confirmation=>“foobar”}
结束
它“应该创建一个用户”吗
lambda do
post:create,:user=>@attr
end.should更改(用户:count).by(1)
结束
它“应该重定向到用户显示页面”吗
post:create,:user=>@attr
response.should将_重定向到(用户_路径(分配(:用户)))
结束
它“应该有欢迎信息”吗
post:create,:user=>@attr
flash[:success].should=~/欢迎使用示例应用程序/i
结束
它“应该让用户登录”吗
post:create,:user=>@attr
controller.com应\u登录
结束
结束
结束
描述“获取‘编辑’”操作
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

            it "should be successful" do
                get :index
                response.should be_success
            end

            it "should have the right title" do
                get :index
                response.should have_selector('title', :content => "All users")
            end

            it "should have an element for each user" do
                get :index
                @users[0..2].each do |user|
                #User.paginate(:page => 1).each do |user|
                    response.should have_selector('li', :content => user.name)
                end
            end

            it "should paginate users" do
                get :index
                response.should have_selector('div.pagination')
                response.should have_selector('span.disabled', :content => "Previous")
                #response.should have_selector('a', :href => "/users?page=2",
                #                                   :content => "2")
                #response.should have_selector('a', :href => "/users?page=2",
                #                                   :content => "Next")
            end
=begin
            it "should have delete links for admins" do
                @user.toggle!(:admin)
                other_user = User.all.second
                get :index
                response.should have_selector('a', :href => user_path(other_user),
                                                   :content => "delete")
            end

            it "should not have delete links for non-admins" do
                other_user = User.all.second
                get :index
                response.should_not have_selector('a', :href => user_path(other_user),
                                                       :content => "delete")
            end
=end
        end
    end

    describe "GET 'show'" do

        before(:each) do
          # used to be just Factory.build(:user)
          @user = Factory(:user)
        end

        it "should be successful" do
          get :show, :id => @user.id
          response.should be_success
        end

        it "should find the right user" do
          get :show, :id => @user.id

          # assigns(:user) returns the 
          # value of the instance variable @user
          assigns(:user).should == @user
        end

        it "should have the right title" do
          get :show, :id => @user.id
          response.should have_selector('title', :content => @user.name)
        end

        it "should have the user's name" do
          get :show, :id => @user.id
          response.should have_selector('h1', :content => @user.name)
        end

        it "should have a profile image" do
          get :show, :id => @user.id
          response.should have_selector('h1>img', :class => "gravatar")
        end
    end

    describe "GET 'new'" do

        it "should be successful" do
          get :new
          response.should be_success
        end

        it "should have the right title" do
          get :new
          response.should have_selector('title', :content => "Sign Up")
        end
    end

    describe "POST 'create'" do

        describe "failure" do

            before(:each) do
                @attr = { :name => "", :email => "", :password => "",
                      :password_confirmation => "" }
            end

            it "should not create a user" do
                lambda do
                    post :create, :user => @attr
                end.should_not change(User, :count)
            end

            it "should have the right title" do
                post :create, :user => @attr
                response.should have_selector('title', :content => "Sign up")
            end

            it "should render the 'new' page" do
                post :create, :user => @attr
                response.should render_template('new')
            end
        end

        describe "success" do

            before(:each) do
                @attr = { :name => "New User", :email => "user@example.com",
                      :password => "foobar", :password_confirmation => "foobar" }
            end

            it "should create a user" do
                lambda do
                    post :create, :user => @attr
                end.should change(User, :count).by(1)
            end

            it "should redirect to the user show page" do
                post :create, :user => @attr
                response.should redirect_to(user_path(assigns(:user)))
            end

            it "should have a welcome message" do
                post :create, :user => @attr
                flash[:success].should =~ /welcome to the sample app/i
            end

            it "should sign the user in" do
                post :create, :user => @attr
                controller.should be_signed_in
            end

        end
    end

    describe "GET 'edit'" do

        before(:each) do
            @user = Factory(:user)
            test_sign_in(@user)
        end

        it "should be successful" do
            get :edit, :id => @user
            response.should be_success
        end

        it "should have the right title" do
            get :edit, :id => @user
            response.should have_selector('title', :content => "Edit user")
        end

        it "should have a link to change the Gravatar" do
            get :edit, :id => @user
            gravatar_url = "http://gravatar.com/emails"
            response.should have_selector('a', :href => 'http://gravatar.com/emails',
                                             :content => "change")
        end
    end



    describe "PUT 'update'" do

        before(:each) do
            @user = Factory(:user)
            test_sign_in(@user)
        end

        describe "failure" do

            before(:each) do
                @attr = { :email => "", :name => "", :password => "",
                          :password_confirmation => "" }
            end

            it "should render the 'edit' page" do
                put :update, :id => @user, :user => @attr
                response.should render_template('edit')
            end

            it "should have the right title" do
                put :update, :id => @user, :user => @attr
                response.should have_selector('title', :content => "Edit user")
            end
        end

        describe "success" do

            before(:each) do
                @attr = { :name => "New Name", :email => "user@example.org",
                          :password => "barbaz", :password_confirmation => "barbaz" }
            end

            it "should change the user's attributes" do
                put :update, :id => @user, :user => @attr
                @user.reload
                @user.name.should  == @attr[:name]
                @user.email.should == @attr[:email]
                #@user.encrypted_password.should == assigns(:user).encrypted_password
            end

            it "should redirect to the user show page" do
                put :update, :id => @user, :user => @attr
                response.should redirect_to(user_path(@user))
            end

            it "should have a flash message" do
                put :update, :id => @user, :user => @attr
                flash[:success].should =~ /updated/
            end
        end
    end

    describe "authentication of edit/update actions" do

        before(:each) do
            @user = Factory(:user)
        end

        describe "for non-signed-in users" do

            it "should deny access to 'edit'" do
                get :edit, :id => @user
                response.should redirect_to(signin_path)
                #flash[:notice].should =~ /sign in/i
            end

            it "should deny access to 'update'" do
                put :update, :id => @user, :user => {}
                response.should redirect_to(signin_path)
            end
        end

        describe "for signed-in users" do

            before(:each) do
                wrong_user = Factory(:user, :email => "user@example.net")
                test_sign_in(wrong_user)
            end

            it "should require matching users for 'edit'" do
                get :edit, :id => @user
                response.should redirect_to(root_path)
            end

            it "should require matching users for 'update'" do
                put :update, :id => @user, :user => {}
                response.should redirect_to(root_path)
            end
        end
    end
end
FactoryGirl.define do

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

  factory :user do
    name "Pavan Katepalli"
    password "foobar"
    password_confirmation "foobar"
    email { FactoryGirl.generate(:email) }
  end

end