2011-06-14 41 views
0

我正在學習rails教程,希望對RSPEC錯誤有所幫助。RSPEC測試失敗,應用程序工程

錯誤:

1) UsersController GET 'index' for signed-in users should be successful 
    Failure/Error: Factory(:user, :name => Factory.next(:name), 
    RuntimeError: 
     No such sequence: name 
    # ./spec/controllers/users_controller_spec.rb:24 
    # ./spec/controllers/users_controller_spec.rb:23:in `times' 
    # ./spec/controllers/users_controller_spec.rb:23 

2) UsersController GET 'index' for signed-in users should have an element for each user 
Failure/Error: Factory(:user, :name => Factory.next(:name), 
RuntimeError: 
    No such sequence: name 
# ./spec/controllers/users_controller_spec.rb:24 
# ./spec/controllers/users_controller_spec.rb:23:in `times' 
# ./spec/controllers/users_controller_spec.rb:23 

    3) UsersController GET 'index' for signed-in users should paginate users 
Failure/Error: Factory(:user, :name => Factory.next(:name), 
RuntimeError: 
    No such sequence: name 
# ./spec/controllers/users_controller_spec.rb:24 
# ./spec/controllers/users_controller_spec.rb:23:in `times' 
    # ./spec/controllers/users_controller_spec.rb:23 

    4) UsersController GET 'index' for signed-in users should have delete links for admins 
Failure/Error: Factory(:user, :name => Factory.next(:name), 
RuntimeError: 
    No such sequence: name 
# ./spec/controllers/users_controller_spec.rb:24 
# ./spec/controllers/users_controller_spec.rb:23:in `times' 
    # ./spec/controllers/users_controller_spec.rb:23 

    5) UsersController GET 'index' for signed-in users should not have delete links for non-admins 
Failure/Error: Factory(:user, :name => Factory.next(:name), 
RuntimeError: 
    No such sequence: name 
# ./spec/controllers/users_controller_spec.rb:24 
# ./spec/controllers/users_controller_spec.rb:23:in `times' 
    # ./spec/controllers/users_controller_spec.rb:23 

* UPDATE:這些錯誤並沒有消失! *

6) UsersController DELETE 'destroy' as an admin user should redirect to the users page 
Failure/Error: response.should redirect_to(users_path) 
    Expected response to be a redirect to <http://test.host/users> but was a redirect to <http://test.host/users/1>. 
    # ./spec/controllers/users_controller_spec.rb:287 

    7) User has_password? method should be true if the passwords match 
Failure/Error: @user.has_password?(@attr[:password]).should be_true 
NoMethodError: 
    undefined method `has_password?' for nil:NilClass 
    # ./spec/models/user_spec.rb:103 

    8) User has_password? method should be false if the passwords don't match 
Failure/Error: @user.has_password?("invalid").should be_false 
NoMethodError: 
    undefined method `has_password?' for nil:NilClass 
    # ./spec/models/user_spec.rb:107 

    9) FriendlyForwardings should forward to the requested page after signin 
    Failure/Error: response.should render_template('users/edit') 
    expecting <"users/edit"> but rendering with <"layouts/_footer, layouts/application,  layouts/_stylesheets, users/show, sessions/new, layouts/_header"> 
    # ./spec/requests/friendly_forwardings_spec.rb:11 

用戶控制器規格:

require 'spec_helper' 

describe UsersController do 
    render_views 

    describe "GET 'index'" do 

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

      30.times do 
       Factory(:user, :name => Factory.next(:name), 
          :email => Factory.next(:email)) 
     end 
    end 

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

    it "should have an element for each user" do 
     get :index 
     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 

    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 

    describe "GET 'show'" do 

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

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

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

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

it "should have a profile image" do 
    get :show, :id => @user 
    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 
    end 

    describe "POST 'create'" do 

describe "failure" do 

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

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


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


describe "success" do 

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

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

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

    it "should sign the user in" do 
    post :create, :user => @attr 
    controller.should be_signed_in 
    end 
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_cofirmation => "" } 
    end 

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

describe "success" do 

    before(:each) do 
    @attr = { :name => "New Name", :email => "[email protected]", :password => "newpassword", :password_confirmation => "newpassword" } 
    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] 
    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 pages" 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) 
    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 

    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 a link to change the Gravatar" do 
    get :edit, :id => @user 
    gravatar_url = "http://gravatar.com/emails" 
    response.should have_selector("a", :href => gravatar_url, :content => "change") 
end 
    end 

    describe "DELETE 'destroy'" do 

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

    describe "as a non-signed-in user" do 
    it "should deny access" do 
     delete :destroy, :id => @user 
     response.should redirect_to(signin_path) 
    end 
    end 

    describe "as a non-admin user" do 
    it "should protect the page" do 
     test_sign_in(@user) 
     delete :destroy, :id => @user 
     response.should redirect_to(root_path) 
    end 
     end 

     describe "as an admin user" do 

    before(:each) do 
     admin = Factory(:user, :email => "[email protected]", :admin => true) 
     test_sign_in(admin) 
    end 

    it "should destroy the user" do 
     lambda do 
     delete :destroy, :id => @user 
     end.should change(User, :count).by(-1) 
    end 

     it "should redirect to the users page" do 
     delete :destroy, :id => @user 
     flash[:success].should =~ /destroyed/i 
     response.should redirect_to(users_path) 
    end 
    end 
    end 
end 

用戶規格:

require 'spec_helper' 

describe User do 

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

    it "should create a new instance given valid attribute" do 
    User.create!(@attr) 
    end 

    it "should require a name" do 
    no_name_user = User.new(@attr.merge(:name => "")) 
    no_name_user.should_not be_valid 
    end 

    it "should require an email address" do 
    no_email_user = User.new(@attr.merge(:email => "")) 
    no_email_user.should_not be_valid 
    end 

    it "should reject names that are too long" do 
    long_name = "a" *51 
    long_name_user = User.new(@attr.merge(:name => long_name)) 
    long_name_user.should_not be_valid 
    end 

    it "should accept valid email addresses" do 
    addresses = %w[[email protected] [email protected] [email protected]] 
     addresses.each do |address| 
      valid_email_user = User.new(@attr.merge(:email => address)) 
      valid_email_user.should be_valid 
     end 
    end 


    it "should reject invalid email addresses" do 
     addresses = %w[[email protected],com user_at_foo.org [email protected]] 
     addresses.each do |address| 
      invalid_email_user = User.new(@attr.merge(:email => address)) 
      invalid_email_user.should_not be_valid 
     end 
    end  

    it "should reject duplicate email addresses" do 
     # Put a user with given email address into the database. 
     User.create!(@attr) 
     user_with_duplicate_email = User.new(@attr) 
     user_with_duplicate_email.should_not be_valid 
    end 

    it "should reject email addresses idential up to the case" do 
    upcased_email = @attr[:email].upcase 
    User.create!(@attr.merge(:email => upcased_email)) 
    user_with_duplicate_email = User.new(@attr) 
    user_with_duplicate_email.should_not be_valid 
     end 

    describe "password validations" do 

it "should require a password" do 
    User.new(@attr.merge(:password => "", :password_confirmation => "")). 
    should_not be_valid 
end 

it "should require a matching password confirmation" do 
    User.new(@attr.merge(:password_confirmation => "invalid")). 
    should_not be_valid 
end 

it "should reject short passwords" do 
    short = "a" * 5 
    hash = @attr.merge(:password => short, :password_confirmation => short) 
    User.new(hash).should_not be_valid 
end 

it "should reject long passwords" do 
    long = "a" * 41 
    hash = @attr.merge(:password => long, :password_confirmation => long) 
    User.new(hash).should_not be_valid 
end 
    end 

    describe "password encryption" do 

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

it "should set the encrypted password attribute" do 
    @user.encrypted_password.should_not be_blank 
end 

it "should have an encrypted password attribute" do 
    @user.should respond_to(:encrypted_password) 
end 
    end 

    describe "has_password? method" do 

    it "should be true if the passwords match" do 
    @user.has_password?(@attr[:password]).should be_true 
    end 

    it "should be false if the passwords don't match" do 
    @user.has_password?("invalid").should be_false 
    end 
    end 

    describe "authenticate method" do 

    it "should return nil on email/password mismatch" do 
    wrong_password_user = User.authenticate(@attr[:email], "wrongpass") 
    wrong_password_user.should be_nil 
    end 

    it "should return nil for an email address with no user" do 
    nonexistent_user = User.authenticate("[email protected]", @attr[:password]) 
    nonexistent_user.should be_nil 
    end 

    it "should return the user on email/password match" do 
    matching_user = User.authenticate(@attr[:email], @attr[:password]) 
    matching_user.should == @user 
    end 
    end 

    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 
end 

場所轉發規格:

require 'spec_helper' 

describe "FriendlyForwardings" do 

    it "should forward to the requested page after signin" do 
    user = Factory(:user) 
    visit edit_user_path(user) 
    fill_in :email, :with => user.email 
    fill_in :password, :with => user.password 
    click_button 
    response.should render_template('users/edit') 
    visit signout_path 
    visit signin_path 
    fill_in :email, :with => user.email 
    fill_in :password, :with => user.password 
    click_button 
    response.should render_template('users/show') 
    end 
end 

工廠:

Factory.define :user do |user| 
    user.name     "Michael Hartl" 
    user.email     "[email protected]" 
    user.password    "foobar" 
    user.password_confirmation "foobar" 
end 

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

我從經驗中發現它可以很容易使一個小錯字,這是否當遵循教程。如果您可以下載完整的源代碼,請嘗試使用BeyondCompare等比較工具來檢查您的代碼與本教程中的源代碼。 – cordsen 2011-06-14 17:23:36

回答

1

只需添加到您的factory.rb像

Factory.sequence :name do |n| 
    "person-#{n}" 
end 
+0

已經完成!任何其他想法? – jsttn 2011-06-14 17:35:53

+0

嘿,在你的測試中你要求:名字序列,但是定義:電子郵件序列 – xandox 2011-06-14 17:39:32

+0

對不起,你能澄清一下嗎?我是新來的鐵軌。 – jsttn 2011-06-14 17:47:27