Jared
Jared

Reputation: 21

When testing a single attribute in rails, get "can't be nil" errors for all other attributes

So, I have an unusual problem I don't really know how to fix. By nature, it's difficult to search for, so I'll try to describe as best I can and provide examples. When I test a single attribute in rspec for any model with a specific thing like numericality or uniqueness, I get an error saying that the other attributes cannot be blank instead of the one error rspec is expecting. I'll post my user model, as this is the simplest.

Model:

class User < ActiveRecord::Base
  validates :name, :email, :password, presence: true
  # validates :email, uniqueness: true
end

Factory:

FactoryGirl.define do
  factory :user do
    name { "Sir Artorias" }
    email { "[email protected]" }
    password { "AGreatPassword!" }
  end
end

Spec:

require "spec_helper"

describe User do
  describe "attributes" do
    user = FactoryGirl.create(:user)
    it { should validate_presence_of(:name) }
    it { should validate_presence_of(:email) }
    it { should validate_uniqueness_of(:email) }
    it { should allow_value("e@e").for(:email) }
    it { should_not allow_value("emailatexample.com").for(:email) }
    it "stores all emails as downcase with white space truncated" do
      user = create(:user, email: " Jo hn.Do e @exa mp le.c om ")
      user.email.should == "[email protected]"
    end
    it { should validate_presence_of(:password) }
    it { should ensure_length_of(:password).is_at_least(8) }
  end

  describe "password is encrypted" do
    user = FactoryGirl.create(:user)
    subject { user.password }
    it { should_not == "AGreatPassword!" }
  end
end

Error example from using above spec:

  5) User attributes should ensure password has a length of at least 8
     Failure/Error: it { should ensure_length_of(:password).is_at_least(8) }

       Expected errors to include "is too short (minimum is 8 characters)" when password is set to "xxxxxxx", got errors: ["name can't be blank (nil)", "email can't be blank (nil)"]

     # ./spec/models/user_spec.rb:17:in `block (3 levels) in <top (required)>'

And this is what I'm referring to. I try to test a length restriction on password, and it errors out telling me that name cannot be nil and email cannot be nil. But I am using a factory above, and other tests which validate the presence of, say, name, don't fail.

Help would be very much appreciated. I can also post anything else you're curious about and think would help, i.e.; gemfile, rspec config, etc.

Upvotes: 0

Views: 412

Answers (1)

Nick Veys
Nick Veys

Reputation: 23939

It's not telling you those errors are a problem, it's telling you it expected an entry saying the password was too short, but didn't. It then dumps the errors the array did contain so you can inspect.

Which makes sense, because in your model you are not validating length:

validates :name, :email, :password, presence: true

Try adding this to your User class:

validates_length_of :password, minimum: 8

Upvotes: 1

Related Questions