我在这里关注教程:http://apionrails.icalialabs.com/book/chapter_five
我正在测试用户模型,并在密码确认时出错。我提供了password
和password_confirmation
,但它仍抱怨没有password_confirmation。
我的users.rb
工厂
FactoryGirl.define do
factory :user do
email { FFaker::Internet.email }
password "12345678"
password_confirmation "12345678"
end
end
我的测试代码user_spec.rb
:
require 'spec_helper'
describe User do
before { @user = FactoryGirl.build(:user) }
subject { @user }
it { should respond_to(:email) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should be_valid }
it { should validate_presence_of(:email) }
it { should validate_uniqueness_of(:email) }
it { should validate_confirmation_of(:password) }
it { should allow_value('example@domain.com').for(:email) }
it { should respond_to(:auth_token) }
it { should validate_uniqueness_of(:auth_token)}
describe "#generate_authentication_token!" do
it "generates a unique token" do
Devise.stub(:friendly_token).and_return("auniquetoken123")
@user.generate_authentication_token!
expect(@user.auth_token).to eql "auniquetoken123"
end
it "generates another token when one already has been taken" do
existing_user = FactoryGirl.create(:user, auth_token: "auniquetoken123")
@user.generate_authentication_token!
expect(@user.auth_token).not_to eql existing_user.auth_token
end
end
end
我的模型user.rb
class User < ApplicationRecord
validates :auth_token, uniqueness: true
validates_confirmation_of :password
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :confirmable
before_create :generate_authentication_token!
validate :password_must_match
def password_must_match
errors.add(:password, "doesn't match confirmation") if password != password_confirmation
end
def generate_authentication_token!
begin
self.auth_token = Devise.friendly_token
end while self.class.exists?(auth_token: auth_token)
end
end
我已在config/application.rb
config.action_mailer.default_url_options = { :host => 'localhost' }
这是我的用户架构:
ActiveRecord::Schema.define(version: 20170505035547) do
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "auth_token", default: ""
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.index ["auth_token"], name: "index_users_on_auth_token", unique: true
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
我得到的错误:
Randomized with seed 21558
...F........
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 Password" (attribute: password_confirmation, value: "some value")
* "doesn't match confirmation" (attribute: password, value: "different value")
# ./spec/models/user_spec.rb:15:in `block (2 levels) in <top (required)>'
Deprecation Warnings:
Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /Users/huanlin/Desktop/dummy/market_api/spec/models/user_spec.rb:23:in `block (3 levels) in <top (required)>'.
If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
1 deprecation warning total
Finished in 0.49896 seconds (files took 2.32 seconds to load)
12 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:15 # User should require password_confirmation to match password
Randomized with seed 21558