b.herring
b.herring

Reputation: 643

testing with Rails Rspec - expected and got aren't matching

Im quite new to Rspec with Rails. Im just trying to run some tests to see if a new user has been saved and if a name, email and password has been entered but it is not working for some reason as no tests are passing. I'm going by this youtube video https://www.youtube.com/watch?v=71eKcNxwxVY. Ive copied everything apart from i didn't use a scaffold. I made a user model myself. thanks

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: "[email protected]", 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: "[email protected]", password: "123123", created_at: nil, updated_at: nil>
     
       (compared using ==)
     
       Diff:
       @@ -1,2 +1,8 @@
       -true
       +#<User:0x00007fa8efdbb628
       + id: nil,
       + name: "ben",
       + email: "[email protected]",
       + 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: "[email protected]", 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: "[email protected]", name: "ben")
        expect(user).to eq(false)
    end

    it 'should save' do
        user = User.new(email: "[email protected]", name: "ben", password: "123123")
        expect(user).to eq(true)
      end
end
end

Upvotes: 0

Views: 1587

Answers (2)

Faruk Hossain
Faruk Hossain

Reputation: 597

Since you are testing validity of User object try using valid? method. If an user is not valid then user.valid? would return false. If it is valid then it would return true.

Here is an example

    it 'ensures email presence' do
      user = User.new(name:"ben", password: "123123")
      expect(user.valid?).to eq(false)
    end

for validation tests you can also use shoulda-matchers.

Upvotes: 0

hashrocket
hashrocket

Reputation: 2222

When writing your tests, you are comparing the entire user object. You should instead compare the attribute you are looking for. Try something like this:

it "ensures name presence" do
  user = User.new(email: "[email protected]", password: "123123")
  expect(user.name).to be_nil
end

As you can see, I've added expect(user.name) instead of just expect(user). Also, just for your information, when you use .new, it doesn't actually save the instance.

Upvotes: 1

Related Questions