Ruby on rails 得到一个;没想到……”;尝试使用shoulda matchers/RSpec测试唯一性验证时出错

Ruby on rails 得到一个;没想到……”;尝试使用shoulda matchers/RSpec测试唯一性验证时出错,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我对Rails应用程序的测试还是新手,我正在尝试将一些基本测试添加到我的一个应用程序模型中。更具体地说,我使用的是shoulda matchers 3.0.1和rspec rails 3.4.0 gems。我的测试如下: RSpec.describe User, type: :model do describe 'validations' do # Other tests...... describe 'for email' do it { should vali

我对Rails应用程序的测试还是新手,我正在尝试将一些基本测试添加到我的一个应用程序模型中。更具体地说,我使用的是shoulda matchers 3.0.1和rspec rails 3.4.0 gems。我的测试如下:

RSpec.describe User, type: :model do
  describe 'validations' do
    # Other tests......
    describe 'for email' do
      it { should validate_uniqueness_of(:email).on(:create) }
    end
  end
end
在包含以下内容的模型上:

class User < ActiveRecord::Base
  validates :email, presence: true, length: { maximum: 255 },
                format: { with: VALID_EMAIL },
                uniqueness: { case_sensitive: false }
  # Other stuff for the model....
end
  1) User validations for email should require case sensitive unique value for email
 Failure/Error: should validate_uniqueness_of(:email).on(:create)

   Did not expect errors to include "has already been taken" when email is set to "A",
   got errors:
   * "can't be blank" (attribute: name, value: nil)
   * "is invalid" (attribute: email, value: "A")
   * "has already been taken" (attribute: email, value: "A")
   * "can't be blank" (attribute: password, value: nil)
   * "is too short (minimum is 6 characters)" (attribute: password, value: nil)
   * "can't be blank" (attribute: password, value: nil)
 # ./spec/models/user_spec.rb:41:in `block (4 levels) in <top (required)>'
class用户
当我运行测试时,它失败,原因如下:

class User < ActiveRecord::Base
  validates :email, presence: true, length: { maximum: 255 },
                format: { with: VALID_EMAIL },
                uniqueness: { case_sensitive: false }
  # Other stuff for the model....
end
  1) User validations for email should require case sensitive unique value for email
 Failure/Error: should validate_uniqueness_of(:email).on(:create)

   Did not expect errors to include "has already been taken" when email is set to "A",
   got errors:
   * "can't be blank" (attribute: name, value: nil)
   * "is invalid" (attribute: email, value: "A")
   * "has already been taken" (attribute: email, value: "A")
   * "can't be blank" (attribute: password, value: nil)
   * "is too short (minimum is 6 characters)" (attribute: password, value: nil)
   * "can't be blank" (attribute: password, value: nil)
 # ./spec/models/user_spec.rb:41:in `block (4 levels) in <top (required)>'
1)电子邮件的用户验证应要求电子邮件具有区分大小写的唯一值
失败/错误:应验证(:电子邮件)的唯一性。在(:创建)
当电子邮件设置为“A”时,不希望错误包括“已被采取”,
出现错误:
*“不能为空”(属性:名称,值:nil)
*“无效”(属性:电子邮件,值:“A”)
*“已采取”(属性:电子邮件,值:“A”)
*“不能为空”(属性:密码,值:nil)
*“太短(最少6个字符)”(属性:密码,值:nil)
*“不能为空”(属性:密码,值:nil)
#./spec/models/user_spec.rb:41:in'block(4层)in'

我不知道在这里该做什么。shoulda matchers文档非常棒,它确实说明了创建初始对象可能会有问题,他们建议在测试之前显式创建一个,这不会改变我的情况,不管怎样,我都会得到错误。我一定是做错了什么,正如我说的,我还没有这种测试的经验。非常感谢您的指导。

好的,我快速检查了一下,发现这是因为您设置了
验证。。。唯一性:{区分大小写:false}
在您的模型中,但您的规范没有相应地指定。你可以试试这个:

it { should validate_uniqueness_of(:email).case_insensitive }

另外,我认为(:create)
上的
在这里是不需要的。

就这样,我欠Stack Overflow的另一个人一个冷冰冰的人情。谢谢你,我的朋友,就这些。