Jan*_*Jan 1 ruby rspec ruby-on-rails ruby-on-rails-3
我正在关注Hartls Rails教程,我在第6章结束时陷入困境.突然之间,我的一些测试失败并出现同样的错误:
C:\Users\JMA\Projekte\sampleapp>bundle exec rspec spec/
.F......F...FFF....
Failures:
1) User
Failure/Error: it { should respond_to(:passwors_confirmation)}
expected #<User id: nil, name: "Example User", email: "user@example.com",
created_at: nil, updated_at: nil, password_digest: "$2a$04$OMnlZgC9yWhxbQizs8MG
9uPMCd8LmMlO5MZzkuQyM8it..."> to respond to :passwors_confirmation
# ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'
2) User when name is not present when email format is valid should be valid
Failure/Error: expect(@user).to be_valid
expected #<User id: nil, name: " ", email: "user@foo.COM", created_at: ni
l, updated_at: nil, password_digest: "$2a$04$PtGxi4DjJWFezLFYy1jW5eaI7C/xxyBLXAG
zPepflo8r..."> to be valid, but got errors: Name can't be blank
# ./spec/models/user_spec.rb:51:in `block (5 levels) in <top (required)>'
# ./spec/models/user_spec.rb:49:in `each'
# ./spec/models/user_spec.rb:49:in `block (4 levels) in <top (required)>'
3) User when name is not present return value of authenticate method with vali
d password
Failure/Error: before { @user.safe }
NoMethodError:
undefined method `safe' for #<User:0x502bc30>
# ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'
4) User when name is not present return value of authenticate method with inva
lid password
Failure/Error: before { @user.safe }
NoMethodError:
undefined method `safe' for #<User:0x50333a0>
# ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'
5) User when name is not present return value of authenticate method with inva
lid password
Failure/Error: before { @user.safe }
NoMethodError:
undefined method `safe' for #<User:0x503b848>
# ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'
Finished in 0.52003 seconds
19 examples, 5 failures
Failed examples:
rspec ./spec/models/user_spec.rb:16 # User
rspec ./spec/models/user_spec.rb:47 # User when name is not present when email f
ormat is valid should be valid
rspec ./spec/models/user_spec.rb:90 # User when name is not present return value
of authenticate method with valid password
rspec ./spec/models/user_spec.rb:96 # User when name is not present return value
of authenticate method with invalid password
rspec ./spec/models/user_spec.rb:97 # User when name is not present return value
of authenticate method with invalid password
Randomized with seed 37132
Run Code Online (Sandbox Code Playgroud)
我的user_specs.rb看起来像这样:
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:passwors_confirmation)}
it { should be_valid }
describe"when name is not present" do
before { @user.name = " " }
it { should_not be_valid}
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid}
end
describe "when name is to 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
adresses = %w[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
adresses.each do |invalid_adress|
@user.email = invalid_adress
expect(@user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
addresses.each do |valid_address|
@user.email = valid_address
expect(@user).to be_valid
end
end
end
describe "when email adress 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}
end
it { should respond_to(:authenticate) }
describe "when password is not present" do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's to short" do
before { @user.password = @user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { @user.safe }
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_inavlid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_inavlid_password }
specify { expect(user_for_inavlid_password).to be_false }
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
users.rb的
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence:true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end
Run Code Online (Sandbox Code Playgroud)
将密码和password_confirmation属性添加到User模型后出现.那里经过的地方直到那时.我继续学习本教程,因为我已经阅读了有关此失败的内容,并将在本教程的后续阶段处理.现在我的测试仍然失败,我不知道为什么.有任何想法吗?
堆栈跟踪说:
Failure/Error: before { @user.safe }
NoMethodError:
undefined method `safe' for #<User:0x503b848>
# ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
它告诉你一些关键的信息:
user_spec.rb
.before { @user.safe }
.NoMethodError
这意味着您正在尝试在未定义该方法的对象上调用方法.safe
为#<User:0x503b848>
它是一个实例User
类.从上下文中,您应该能够推断出它是指@user
在此上下文中的变量.这是方式更比足够的信息来推断什么原因的问题是:你打电话@user.safe
上线86,并没有这样的方法safe
进行@user
.
问题的解决方案是你可能打算打电话save
,而不是safe
.
由于其他一些原因,您还有其他一些故障.看看您是否可以阅读错误消息中的信息以了解问题的原因,然后应该很容易弄清楚如何解决这些问题.