Ruby on rails rails设计验证密码\u确认不起作用

Ruby on rails rails设计验证密码\u确认不起作用,ruby-on-rails,devise,Ruby On Rails,Devise,我在设计验证方面遇到了问题。这是我的模型类(注意,我已经启用了validatable): 我的工厂: FactoryGirl.define do factory :user do email { FFaker::Internet.email } password "12345678" password_confirmation "12345678" end end 但我有一个错误: 1) User should require password_confir

我在设计验证方面遇到了问题。这是我的模型类(注意,我已经启用了
validatable
):

我的工厂:

FactoryGirl.define do
  factory :user do
    email { FFaker::Internet.email }
    password "12345678"
    password_confirmation "12345678"
  end
end
但我有一个错误:

  1) User should require password_confirmation to match password
     Failure/Error: it { should validate_confirmation_of(:password) }

   Expected errors to include "doesn't match Password" when password is set to "different value",
   got no errors
 # ./spec/models/user_spec.rb:18:in `block (2 levels) in <top (required)>'
Failures:

  1) User should require password_confirmation to match password
     Failure/Error: it { should validate_confirmation_of(:password) }

       Expected errors to include "doesn't match Password" when password is set to "different value",
       got errors:
       * "doesn't match Password" (attribute: password_confirmation, value: "some value")
       * "doesn't match confirmation" (attribute: password, value: "different value")
     # ./spec/models/user_spec.rb:18:in `block (2 levels) in <top (required)>'
得到了这个错误:

  1) User should require password_confirmation to match password
     Failure/Error: it { should validate_confirmation_of(:password) }

   Expected errors to include "doesn't match Password" when password is set to "different value",
   got no errors
 # ./spec/models/user_spec.rb:18:in `block (2 levels) in <top (required)>'
Failures:

  1) User should require password_confirmation to match password
     Failure/Error: it { should validate_confirmation_of(:password) }

       Expected errors to include "doesn't match Password" when password is set to "different value",
       got errors:
       * "doesn't match Password" (attribute: password_confirmation, value: "some value")
       * "doesn't match confirmation" (attribute: password, value: "different value")
     # ./spec/models/user_spec.rb:18:in `block (2 levels) in <top (required)>'

它是有效的。但是我不想从shoulda

重新发明轮子。在你的第一次测试中,你希望它能验证密码确认,但你通过了相同的密码,所以它永远不会失败。所以你有3个选择。1使用您的第一次测试,但将确认更改为不相同(通过factory girl传递参数)。2您可以期望引发(如果您这样做,您应该指定将引发的错误)。或者您也可以使用Factory Girl的构建方法,并期望用户无效

Failures:

  1) User should require password_confirmation to match password
     Failure/Error: it { should validate_confirmation_of(:password) }

       Expected errors to include "doesn't match Password" when password is set to "different value",
       got errors:
       * "doesn't match Password" (attribute: password_confirmation, value: "some value")
       * "doesn't match confirmation" (attribute: password, value: "different value")
     # ./spec/models/user_spec.rb:18:in `block (2 levels) in <top (required)>'
  describe "#confirmation_of_password" do
    it "should fail when password does not match" do
      expect { User.create!(email: "xxx@xxx.com", password: "123456", password_confirmation: "1234567") }.to raise_error
    end
  end