我想很多人都问过这个问题,但遗憾的是我并不理解他们给出的答案。基本上,我需要你们只是说,“这就是你需要粘贴的代码”,然后向我解释,因为坦率地说,我没有得到它。
好的,这就是问题所在。我正在为我的用户模型运行测试,这就是:
1) User return value of authenticate method with valid password
Failure/Error: before { @user.save }
RuntimeError:
Password digest missing on new record
# ./spec/models/user_spec.rb:61:in `block (3 levels) in <top (required)>'
2) User return value of authenticate method with invalid password
Failure/Error: before { @user.save }
RuntimeError:
Password digest missing on new record
# ./spec/models/user_spec.rb:61:in `block (3 levels) in <top (required)>'
3) User return value of authenticate method with invalid password
Failure/Error: before { @user.save }
RuntimeError:
Password digest missing on new record
# ./spec/models/user_spec.rb:61:in `block (3 levels) in <top (required)>'
现在我的代码。这是我的rspec:
require 'spec_helper'
describe User do
before do
@user = User.new(first: "Example", last: "Example", email: "user@andover.edu", password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:first) }
it { should respond_to(:last) }
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.first = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.first = "a" * 51 }
it { should_not be_valid }
end
describe "when name is not present" do
before { @user.last = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.last = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.foo@bar_baz.com foo@bar+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[cwolford@andover.edu]
addresses.each do |valid_address|
@user.email = valid_address
expect(@user).to be_valid
end
end
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 eq found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
end
这是我的用户模型:
class User < ActiveRecord::Base
attr_accessible :first, :last, :email, :password, :password_confirmation
attr_accessor :password, :password_confirmation
has_secure_password
before_save { self.email = email.downcase }
validates :first, :last, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[andover]+\.[edu]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
end
这是我的宝石文件:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '~> 3.1.2'
gem "protected_attributes", :github => "rails/protected_attributes"
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
我不会通过发布我的架构来浪费更多空间,但我确实想说我把它放在架构中:t.string“password_digest”。我知道这对某些人来说是一个问题,因为他们离开了。
你的想法?
答案 0 :(得分:3)
<强>解决方案:强>
从attr_accessor :password, :password_confirmation
模型中移除User
。
<强>原因:强>
首先,了解attr_accessor
和attr_accessible
之间的区别 - 对SO已经有很多很好的解释,例如:
Difference between attr_accessor and attr_accessible
由于ruby一次解释User
模型一行:
attr_accessible :first, :last, :email, :password, :password_confirmation
哪个好.....直到最后一行:
attr_accessor :password, :password_confirmation
当它覆盖:password
和:password_confirmation
属性以使其attr_accessor
当您拨打@user.save
希望有所帮助。