Skip to content

Instantly share code, notes, and snippets.

@chrislerum
Created January 18, 2015 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislerum/ba0e2180a3e9a38a9840 to your computer and use it in GitHub Desktop.
Save chrislerum/ba0e2180a3e9a38a9840 to your computer and use it in GitHub Desktop.
Failure/Error: it {should validate_presence_of(:last_name)}
NoMethodError:
undefined method `>' for nil:NilClass
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, :last_name, :dob, presence: true
validate :check_age, on: :create
def full_name
[first_name, last_name].join(' ')
end
protected
def check_age
if dob > Time.now - 20.years
flash[:notice] = 'Too Young'
false
else
true
end
end
end
require 'rails_helper'
describe User do
it {should validate_presence_of(:first_name)}
it {should validate_presence_of(:last_name)}
it {should validate_presence_of(:dob)}
it {should respond_to :full_name}
end
@chrislerum
Copy link
Author

I'm trying to have my User invalidate if user is too young (< 20). but when rspec checks, for example, that the last_name is present, it causes my custom validator method to run, and since dob is nil at that point, it crashes thusly.
Any advice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment