2012-09-18 76 views
0

我正在研究迈克尔哈特尔导轨教程。 Currentlly在第6章编辑user_spec.rb文件后,我尝试运行测试,但我得到很大的失败:迈克尔哈特斯教程第6章-User_spec.rb-大规模测试失败

C:\rails_project\sample_app>bundle exec rspec spec/ 
←[32m.←[0m←[32m.←[0m←[33m*←[0m←[31mF←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m 
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m 
←[31mF←[0m←[32m.←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[32m.←[0m←[31mF←[0m 
←[31mF←[0m←[31mF←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m 
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[33m*←[0m 

Pending: 
←[33m UsersHelper add some examples to (or delete) C:/rails_project/sample_app/ 
spec/helpers/users_helper_spec.rb←[0m 
←[36m # No reason given←[0m 
←[36m # ./spec/helpers/users_helper_spec.rb:14←[0m 
←[33m users/new.html.erb add some examples to (or delete) C:/rails_project/samp 
le_app/spec/views/users/new.html.erb_spec.rb←[0m 
←[36m # No reason given←[0m 
←[36m # ./spec/views/users/new.html.erb_spec.rb:4←[0m 

Failures: 

    1) User 
    ←[31mFailure/Error:←[0m ←[31mit { should be_valid }←[0m 
     ←[31mexpected valid? to return true, got false←[0m 
←[36m  # ./spec/models/user_spec.rb:31:in `block (2 levels) in <top (required 
)>'←[0m 

    2) User when email format is valid should be valid 
    ←[31mFailure/Error:←[0m ←[[email protected] be_valid←[0m 
     ←[31mexpected valid? to return true, got false←[0m 
←[36m  # ./spec/models/user_spec.rb:64:in `block (4 levels) in <top (required 
)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:62:in `each'←[0m 
←[36m  # ./spec/models/user_spec.rb:62:in `block (3 levels) in <top (required 
)>'←[0m 

3) User with a password that's too short 
    ←[31mFailure/Error:←[0m ←[31mit { should be_valid }←[0m 
     ←[31mexpected valid? to return true, got false←[0m 
←[36m  # ./spec/models/user_spec.rb:112:in `block (3 levels) in <top (require 
d)>'←[0m 

4) User return value of authenticate method with valid password 
←[31mFailure/Error:←[0m ←[31mit { should == found_user.authenticate(@user.p 
assword) }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:99:in `block (4 levels) in <top (required 
)>'←[0m 

5) User return value of authenticate method with invalid password 
←[31mFailure/Error:←[0m ←[31mlet(:user_for_invalid_password) { found_user.a 
uthenticate("invalid") }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:103:in `block (4 levels) in <top (require 
d)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:105:in `block (4 levels) in <top (require 
d)>'←[0m 

    6) User return value of authenticate method with invalid password 
←[31mFailure/Error:←[0m ←[31mlet(:user_for_invalid_password) { found_user.a 
uthenticate("invalid") }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:103:in `block (4 levels) in <top (require 
d)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:106:in `block (4 levels) in <top (require 
d)>'←[0m 

    7) User return value of authenticate method with valid password 
     ←[31mFailure/Error:←[0m ←[31mit { should == found_user.authenticate(@user.e 
mail) }←[0m 
     ←[31mNoMethodError:←[0m 
     ←[31mundefined method `authenticate' for nil:NilClass←[0m 
    ←[36m  # ./spec/models/user_spec.rb:120:in `block (4 levels) in <top (require 
    d)>'←[0m 

    8) User return value of authenticate method with invalid password 
     ←[31mFailure/Error:←[0m ←[31mlet(:user_for_invalid_password) { found_user.a 
    uthenticate("invalid") }←[0m 
     ←[31mNoMethodError:←[0m 
     ←[31mundefined method `authenticate' for nil:NilClass←[0m 
    ←[36m  # ./spec/models/user_spec.rb:124:in `block (4 levels) in <top (require 
    d)>'←[0m 
    ←[36m  # ./spec/models/user_spec.rb:126:in `block (4 levels) in <top (require 
    d)>'←[0m 

    9) User return value of authenticate method with invalid password 
←[31mFailure/Error:←[0m ←[31mlet(:user_for_invalid_password) { found_user.a 
uthenticate("invalid") }←[0m 
←[31mNoMethodError:←[0m 
    ←[31mundefined method `authenticate' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:124:in `block (4 levels) in <top (require 
d)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:127:in `block (4 levels) in <top (require 
d)>'←[0m 

Finished in 10.71 seconds 
←[31m37 examples, 9 failures, 2 pending←[0m 

Failed examples: 

←[31mrspec ./spec/models/user_spec.rb:31←[0m ←[36m# User ←[0m 
←[31mrspec ./spec/models/user_spec.rb:60←[0m ←[36m# User when email format is va 
lid should be valid←[0m 
←[31mrspec ./spec/models/user_spec.rb:112←[0m ←[36m# User with a password that's 
too short ←[0m 
←[31mrspec ./spec/models/user_spec.rb:99←[0m ←[36m# User return value of authent 
icate method with valid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:105←[0m ←[36m# User return value of authen 
ticate method with invalid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:106←[0m ←[36m# User return value of authen 
ticate method with invalid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:120←[0m ←[36m# User return value of authen 
ticate method with valid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:126←[0m ←[36m# User return value of authen 
ticate method with invalid password ←[0m 
←[31mrspec ./spec/models/user_spec.rb:127←[0m ←[36m# User return value of authen 
ticate method with invalid password ←[0m 

这是我的User_spec.rb文件:

require 'spec_helper' 

describe User do 

before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
        password: "foobar", 
    passsword_confirmation: "foobar") 
end 

subject { @user } 

it { should respond_to(:name) } 
it { should respond_to(:email) } 
it { should respond_to(:password_digest) } 
it { should respond_to(:password) } 
it { should respond_to(:password_confirmation) } 
it { should respond_to(:authenticate) } 

it { should be_valid } 

describe "When name is not present" do 
    before { @user.name = " " } 
    it { should_not be_valid } 
end 

describe "when email is not present" do 
    before { @user.email = " " } 
    it { should_not be_valid} 
end 

describe "when name is too long" do 
    before { @user.name = "a" * 51 } 
    it { should_not be_valid } 
end 

describe "when email format is invalid" do 
    it "should be invalid" do 
     addresses = %w[[email protected],com user_at_foo.org [email protected] 
         [email protected]_baz.com [email protected]+baz.com] 
     addresses.each do |invalid_address| 
      @user.email = invalid_address 
      @user.should_not be_valid 
     end 
    end 
end 

describe "when email format is valid" do 
    it "should be valid" do 
     addresses = %w[[email protected] [email protected] [email protected] 
    [email protected]] 
     addresses.each do |valid_address| 
      @user.email = valid_address 
      @user.should be_valid 
     end 
    end 
end 

describe "when email addresses is already taken" do 
    before do 
     user_with_same_email = @user.dup 
     user_with_same_email.email = @user.email.upcase 
     user_with_same_email.save 
    end 

    it { should_not be_valid } 
end 

describe "when password is not present" do 
    before { @user.password = @user.password_confirmation = " "} 
    it { should_not be_valid } 
end 

describe "when password doesn't match confirmation" do 
    before { @user.password_confirmation = "mismatch" } 
    it { should_not be_valid } 
end 

describe "when password confirmation is nil" do 
    before { @user.password_confirmation = nil } 
    it { should_not be_valid } 
end 

describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by_email(@user.email) } 

    describe "with valid password" do 
     it { should == found_user.authenticate(@user.password) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { 
found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
end 

describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should be_valid } 
end 

describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by_email(@user.email) }  

    describe "with valid password" do 
     it { should == found_user.authenticate(@user.email) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { 
found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
end 
end 

这里是我的问题:

第一个&最明显:

我该怎么做才能通过这个测试?错误消息让我不知道。

当我创建新用户是这样的:

irb(main):002:0> user = User.create(name: "Example user", email: "[email protected]> 
    ←[1m←[36m (0.0ms)←[0m ←[1mbegin transaction←[0m 
    ←[1m←[35mUser Exists (0.0ms)←[0m SELECT 1 AS one FROM "users" WHERE LOWER("us 
ers"."email") = LOWER('[email protected]') LIMIT 1 
    ←[1m←[36m (0.0ms)←[0m ←[1mrollback transaction←[0m 
=> #<User id: nil, name: "Example user", email: "[email protected]", created_at: 
nil, updated_at: nil, password_digest: nil> 

然后我试图挽救这个用户我得到“假”的回应:

irb(main):004:0> user.save 
    ←[1m←[35m (0.0ms)←[0m begin transaction 
    ←[1m←[36mUser Exists (1.0ms)←[0m ←[1mSELECT 1 AS one FROM "users" WHERE LOWER 
    ("users"."email") = LOWER('[email protected]') LIMIT 1←[0m 
    ←[1m←[35m (0.0ms)←[0m rollback transaction 
    => false 

这是问题与一开始提到的测试失败相关联?

最后一个问题:这是什么意思?

←[32m.←[0m←[32m.←[0m←[33m*←[0m←[31mF←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m 
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m 
←[31mF←[0m←[32m.←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[32m.←[0m←[31mF←[0m 
←[31mF←[0m←[31mF←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m 
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[33m*←[0m 
+1

您能否显示您的用户模型?如果您使用的是Windows,请尝试从项目根目录中的.rspec文件中删除--colour选项以避免←[0m垃圾。 – dimuch

回答

0

你的意思是user.rb?

class User < ActiveRecord::Base 
    attr_accessible :email, :name, :password, :password_confirmation 
    has_secure_password 

    before_save { |user| user.email = email.downcase } 

    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
       uniqueness: { case_sensitive: false } 
    validates :password, presence: true, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 

end 
1

BUDDY:注意下面的代码(它是你的代码)。它来自user_spec.rb。您为“无效密码”复制/粘贴两次。其他一切都很好,你只是在spec文件中输入错误。

describe "with invalid password" do 
     let(:user_for_invalid_password) { 
found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
end 

describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should be_valid } 
end 

describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by_email(@user.email) }  

    describe "with valid password" do 
     it { should == found_user.authenticate(@user.email) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { 
found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
end 
end 
相关问题