2014-02-16 134 views
0

我查看了堆栈溢出的答案,发现有些人发布了类似的问题,但解决方案没有帮助。从我可以告诉我的代码是完全正确的。为什么此规范不能按预期工作?

我下面的教程: http://ruby.railstutorial.org/chapters/modeling-users#code-validates_uniqueness_of_email_case_insensitive_test

require 'spec_helper' 

describe User do 

    before { @user = User.new(name: "Example User", email: "[email protected]") } 

    subject { @user } 

    it { should respond_to(:name) } 
    it { should respond_to(:email) } 

    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 
     expect(@user).not_to 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 
     expect(@user).to be_valid 
     end 
    end 
    end 

    describe "when email address 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" do 
    # expect(@user).not_to be_valid 
    #end 
    it { should_not be_valid } 

    end 


end 

最后的规范失败。

我的用户验证是这样:

class User < ActiveRecord::Base 
    before_save { self.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_sensative: false } 
end 

我完全跟着教程,但我似乎无法得到这个验证才能正常工作。我知道这个教程有点老,我可能会使用一些新的宝石。我也知道这不是做这种验证的最好方法,但教程刚开始时非常简单。这里发生了什么?

谢谢!


编辑: 没有错误。验证失败时应该是成功的。

Failures: 
1) User when email address is already taken should not be valid 
Failure/Error: it { should_not be_valid } 
expected #<User id: nil, name: "Example User", email: "[email protected]", created_at: nil, updated_at: nil> not to be valid 
# ./spec/models/user_spec.rb:58:in `block (3 levels in <top (required)>' 

Finished in 0.09887 seconds 
1 example, 1 failure 

再次编辑。 user_spec.rb已被完全粘贴在任务中。抱歉,我对于ruby真的很陌生,并没有意识到可能需要帮助解决问题。

+1

你可以把错误信息? –

+0

期望未能得到满足,还是因为异常引发而失败? –

+0

你在哪里设置主题?换句话说,规范中的“它”是什么?请注意,有问题的电子邮件不是大写的,所以除非你有一个回调/ etc下层 - 它看起来像你测试错误的东西。用-b运行,然后调用save !. –

回答

2

如果您继续阅读本教程,您会发现作者说测试仍未完成通过。这是因为当前的验证仍然区分大小写。

添加uniqueness: { case_sensitive: false }使得通过测试预期

validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
+0

拍摄!谢谢你,但我偶然粘贴了旧代码。发布时我正在修补它。我更新了问题以反映当前的代码。 – Bil1

0

我认为它只是解决了自身。也许有人可以解释刚刚发生的事情。我决定继续教程并将gem 'bcrypt-ruby', '3.1.2'添加到gem文件中。然后我跑了bundle install

然后我试图运行rspec spec/并返回了一个错误:

Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue. 

但是当我跑rake db:migrate什么都没有发生。所以我搜索了一下,发现this SO post其中有a solution

我跑:

rake test:prepare 
rake db:migrate 

,然后所有测试通过。

也许我的测试环境对我很疯狂?我仍然不知道发生了什么。

0

您可以.save前加user_with_same_email.valid?并提供其输出,但我相信这将是true,因为:

  1. 你没有改变的@user

  2. 你只需要改变的id,但它也是有效的,所以“[email protected]”和“[email protected]”是相同的,并且是有效的。

  3. #valid回报true因为:email存在,具有良好的Regexp,仍然是独一无二的。

相关问题