2013-01-21 3 views
5

Continuo a ricevere questo errore quando eseguo rspec: Sequenza non registrata: email.ArgumentError: Sequenza non registrata: email

Tuttavia, l'ho impostato nel file factories.rb. Qualche idea su come risolvere questo problema? L'app sta funzionando bene.

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 protected]" 
    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 => "[email protected]") 
       third = Factory(:user, :email => "[email protected]") 

       @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 => "[email protected]", 
         :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 => "[email protected]", 
          :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 => "[email protected]") 
       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 

risposta

4

Mettere la sequenza all'interno del blocco definire?

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 
+0

grazie, lo sto testando ora. Sto usando la versione 1.0 della factory girl, quindi non sarebbe esattamente quello che hai scritto. –

+0

non ha funzionato :(. Quei test falliscono ancora.Tutti gli altri suggerimenti? –

+0

Qualsiasi motivo per cui non è possibile aggiornare la versione di factory girl? –

Problemi correlati