Created
January 18, 2015 20:24
-
-
Save chrislerum/ba0e2180a3e9a38a9840 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Failure/Error: it {should validate_presence_of(:last_name)} | |
NoMethodError: | |
undefined method `>' for nil:NilClass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?