Created
September 24, 2010 23:00
-
-
Save sukima/596202 to your computer and use it in GitHub Desktop.
shoulda error with validates_each
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
suki[test] (feature/testing)$ ruby unit/instructor_test.rb | |
/Users/suki/.rvm/gems/ruby-1.8.7-p302@rails2.3.5/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning: Gem::Dependency#version_requirements is deprecated and will be removed on or after August 2010. Use #requirement | |
Loaded suite unit/instructor_test | |
Started | |
..EE | |
Finished in 0.146222 seconds. | |
1) Error: | |
test: Instructor should require email to be set. (InstructorTest): | |
NoMethodError: undefined method `[]' for false:FalseClass | |
app/models/instructor.rb:11 | |
shoulda (2.11.3) [v] lib/shoulda/active_record/matchers/allow_value_matcher.rb:61:in `errors_match?' | |
shoulda (2.11.3) [v] lib/shoulda/active_record/matchers/allow_value_matcher.rb:43:in `matches?' | |
shoulda (2.11.3) [v] lib/shoulda/active_record/matchers/validation_matcher.rb:43:in `disallows_value_of' | |
shoulda (2.11.3) [v] lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb:32:in `matches?' | |
shoulda (2.11.3) [v] lib/shoulda/assertions.rb:53:in `assert_accepts' | |
shoulda (2.11.3) [v] lib/shoulda/context.rb:324:in `__bind_1285369213_172216' | |
shoulda (2.11.3) [v] lib/shoulda/context.rb:382:in `call' | |
shoulda (2.11.3) [v] lib/shoulda/context.rb:382:in `test: Instructor should require email to be set. ' | |
2) Error: | |
test: Instructor should require name to be set. (InstructorTest): | |
NoMethodError: undefined method `[]' for false:FalseClass | |
app/models/instructor.rb:11 | |
shoulda (2.11.3) [v] lib/shoulda/active_record/matchers/allow_value_matcher.rb:61:in `errors_match?' | |
shoulda (2.11.3) [v] lib/shoulda/active_record/matchers/allow_value_matcher.rb:43:in `matches?' | |
shoulda (2.11.3) [v] lib/shoulda/active_record/matchers/validation_matcher.rb:43:in `disallows_value_of' | |
shoulda (2.11.3) [v] lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb:32:in `matches?' | |
shoulda (2.11.3) [v] lib/shoulda/assertions.rb:53:in `assert_accepts' | |
shoulda (2.11.3) [v] lib/shoulda/context.rb:324:in `__bind_1285369213_176152' | |
shoulda (2.11.3) [v] lib/shoulda/context.rb:382:in `call' | |
shoulda (2.11.3) [v] lib/shoulda/context.rb:382:in `test: Instructor should require name to be set. ' | |
4 tests, 2 assertions, 0 failures, 2 errors |
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 Instructor < ActiveRecord::Base | |
validates_presence_of :name | |
validates_presence_of :email | |
validates_uniqueness_of :name | |
# Causes test errors. Works in real practice. | |
validates_each :name do |record, attr, value| | |
name_hash = self.parse_name(value) # returns a hash with :first_name and :last_name | |
record.errors.add attr, 'must have a first and last name' if | |
( name_hash[:first_name].nil? || name_hash[:last_name].nil? ) | |
end | |
private | |
def self.parse_name(value) | |
# The code for this method can be found at | |
# http://gist.github.com/491187 | |
# | |
# It returns the following when value = "John Doe" | |
return { | |
:suffix => "", | |
:last_name => "Doe", | |
:prefix => "", | |
:first_name => "John", | |
:middle_name => "" | |
} | |
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 'test_helper' | |
class InstructorTest < ActiveSupport::TestCase | |
should validate_presence_of(:name) | |
should validate_uniqueness_of(:name) | |
should validate_presence_of(:email) | |
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
ruby-1.8.7-p302@rails2.3.5: | |
system: | |
uname: "Darwin Yuzuki 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386 i386" | |
bash: "/bin/bash => GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)" | |
zsh: "/bin/zsh => zsh 4.3.9 (i386-apple-darwin10.0)" | |
rvm: | |
version: "rvm 1.0.11 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]" | |
ruby: | |
interpreter: "ruby" | |
version: "1.8.7" | |
date: "2010-08-16" | |
platform: "i686-darwin10.4.0" | |
patchlevel: "2010-08-16 patchlevel 302" | |
full_version: "ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]" | |
homes: | |
gem: "/Users/suki/.rvm/gems/ruby-1.8.7-p302@rails2.3.5" | |
ruby: "/Users/suki/.rvm/rubies/ruby-1.8.7-p302" | |
binaries: | |
ruby: "/Users/suki/.rvm/rubies/ruby-1.8.7-p302/bin/ruby" | |
irb: "/Users/suki/.rvm/rubies/ruby-1.8.7-p302/bin/irb" | |
gem: "/Users/suki/.rvm/rubies/ruby-1.8.7-p302/bin/gem" | |
rake: "/usr/local/bin/rake" | |
environment: | |
PATH: "/Users/suki/.rvm/gems/ruby-1.8.7-p302@rails2.3.5/bin:/Users/suki/.rvm/gems/ruby-1.8.7-p302@global/bin:/Users/suki/.rvm/rubies/ruby-1.8.7-p302/bin:/Users/suki/.rvm/bin:/usr/local/bin:/usr/local/sbin:/Users/suki/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/Developer/Tools" | |
GEM_HOME: "/Users/suki/.rvm/gems/ruby-1.8.7-p302@rails2.3.5" | |
GEM_PATH: "/Users/suki/.rvm/gems/ruby-1.8.7-p302@rails2.3.5:/Users/suki/.rvm/gems/ruby-1.8.7-p302@global" | |
BUNDLE_PATH: "/Users/suki/.rvm/gems/ruby-1.8.7-p302@rails2.3.5" | |
MY_RUBY_HOME: "/Users/suki/.rvm/rubies/ruby-1.8.7-p302" | |
IRBRC: "/Users/suki/.rvm/rubies/ruby-1.8.7-p302/.irbrc" | |
RUBYOPT: "" | |
gemset: "rails2.3.5" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment