使用Rails Rspec进行测试 - 预期和得到的不匹配
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Rails Rspec进行测试 - 预期和得到的不匹配相关的知识,希望对你有一定的参考价值。
我对Rails的Rspec很新。我只是试图运行一些测试,以查看是否已保存新用户以及是否已输入名称,电子邮件和密码但由于某些原因因为没有测试通过而无法正常工作。我要通过这个youtube视频https://www.youtube.com/watch?v=71eKcNxwxVY。我已经复制了一切,除了我没有使用脚手架。我自己创建了一个用户模型。谢谢
class User < ApplicationRecord
validates :name, presence: true
validates :email, presence: true
validates :password, presence: true
end
rspec
User
validation tests
ensures name presence (FAILED - 1)
ensures email presence (FAILED - 2)
ensures password presence (FAILED - 3)
should save (FAILED - 4)
Failures:
1) User validation tests ensures name presence
Failure/Error: expect(user).to eq()
ArgumentError:
wrong number of arguments (given 0, expected 1)
# ./spec/models/user_spec.rb:7:in `block (3 levels) in <top (required)>'
2) User validation tests ensures email presence
Failure/Error: expect(user).to eq(false)
expected: false
got: #<User id: nil, name: "ben", email: nil, password: "123123", created_at: nil, updated_at: nil>
(compared using ==)
# ./spec/models/user_spec.rb:12:in `block (3 levels) in <top (required)>'
3) User validation tests ensures password presence
Failure/Error: expect(user).to eq(false)
expected: false
got: #<User id: nil, name: "ben", email: "ben@ben.com", password: nil, created_at: nil, updated_at: nil>
(compared using ==)
# ./spec/models/user_spec.rb:17:in `block (3 levels) in <top (required)>'
4) User validation tests should save
Failure/Error: expect(user).to eq(true)
expected: true
got: #<User id: nil, name: "ben", email: "ben@ben.com", password: "123123", created_at: nil, updated_at: nil>
(compared using ==)
Diff:
@@ -1,2 +1,8 @@
-true
+#<User:0x00007fa8efdbb628
+ id: nil,
+ name: "ben",
+ email: "ben@ben.com",
+ password: "123123",
+ created_at: nil,
+ updated_at: nil>
# ./spec/models/user_spec.rb:22:in `block (3 levels) in <top (required)>'
Finished in 0.02486 seconds (files took 1.43 seconds to load)
4 examples, 4 failures
Failed examples:
rspec ./spec/models/user_spec.rb:5 # User validation tests ensures name presence
rspec ./spec/models/user_spec.rb:10 # User validation tests ensures email presence
rspec ./spec/models/user_spec.rb:15 # User validation tests ensures password presence
rspec ./spec/models/user_spec.rb:20 # User validation tests should save
RSpec.describe User, type: :model do
context 'validation tests' do
it "ensures name presence" do
user = User.new(email: "ben@ben.com", password: "123123")
expect(user).to eq(false)
end
it 'ensures email presence' do
user = User.new(name:"ben", password: "123123")
expect(user).to eq(false)
end
it 'ensures password presence' do
user = User.new(email: "ben@ben.com", name: "ben")
expect(user).to eq(false)
end
it 'should save' do
user = User.new(email: "ben@ben.com", name: "ben", password: "123123")
expect(user).to eq(true)
end
end
end
答案
编写测试时,您正在比较整个用户对象。您应该比较您要查找的属性。尝试这样的事情:
it "ensures name presence" do
user = User.new(email: "ben@ben.com", password: "123123")
expect(user.name).to be_nil
end
如你所见,我添加了expect(user.name)
而不仅仅是expect(user)
。另外,仅仅为了您的信息,当您使用.new时,它实际上并不保存实例。
另一答案
由于您正在测试User对象的有效性,请尝试使用valid?
方法。如果用户无效,则user.valid?
将返回false
。如果它是有效的,那么它将返回true
。
这是一个例子
it 'ensures email presence' do
user = User.new(name:"ben", password: "123123")
expect(user.valid?).to eq(false)
end
对于验证测试,您也可以使用shoulda-matchers。
以上是关于使用Rails Rspec进行测试 - 预期和得到的不匹配的主要内容,如果未能解决你的问题,请参考以下文章
失败/错误:需要'rspec/rails'类型错误:错误的参数类型类(预期模块)Rspec V3
.env 未使用 rspec 在 Rails 的测试环境中加载