使用Rails Rspec进行测试-预期和得到的不匹配

时间:2019-01-10 20:38:04

标签: ruby-on-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

2 个答案:

答案 0 :(得分:1)

编写测试时,您正在比较整个用户对象。相反,您应该比较要查找的属性。尝试这样的事情:

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 时,它实际上并不保存实例。

答案 1 :(得分:0)

由于您正在测试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