Hi,
I''m getting invalid results when testing a Regex in a format
validation on a Rails model.
Code is...
# in Model
# allow ''- n/a -'' or alphanum with _ -- no spaces
validates_format_of :test_name, :with => /\A[A-Za-z0-9_]+\z/, :unless
=> lambda{|x| x.test_name == ''- n/a -''}
# in spec
hardware_test = Factory.build(:hardware_test, :test_name => ''- n/a
-'')
hardware_test.should be_valid,
"''#{hardware_test.test_name}'' - should
be valid"
# spec result
3)
''HardwareTest should properly validate test_name format''
FAILED
''- n/a -'' - should be valid
./spec/models/hardware_test_spec.rb:34:
# finally, from console
andy at hst-26-147 (Rails234)->sc
Loading development environment (Rails 2.3.4)>> a = HardwareTest.new(:test_name => ''- n/a -'')
=> #<HardwareTest id: nil, test_name: "- n/a -", code_module_id:
nil,
test_description: nil, test_reviewed: false, interactive: false,
loopback: false, factory: false, media: false, test_category_id: nil,
created_at: nil, updated_at: nil, creator_id: nil, updater_id:
nil>>> a.valid?
=> true
Granted the console is in dev, not test, but I still don''t get it.
Literally pulled on my hair on this one. I''ve tried altering the
Regex many times - adding the :unless => lambda was my last effort.
I can''t see how this isn''t passing.
Thanks for any help
wait, nevermind turns out there was a callback that was being hidden - and triggering a different error all''s well On Nov 21, 9:34?am, Andy Koch <andy.k... at pc-doctor.com> wrote:> Hi, > > I''m getting invalid results when testing a Regex in a format > validation on a Rails model. > > Code is... > > # in Model > # allow ''- n/a -'' or alphanum with _ -- no spaces > validates_format_of :test_name, :with => /\A[A-Za-z0-9_]+\z/, :unless > => lambda{|x| x.test_name == ''- n/a -''} > > # in spec > hardware_test = Factory.build(:hardware_test, :test_name => ''- n/a -'') > hardware_test.should be_valid, "''#{hardware_test.test_name}'' - should > be valid" > > # spec result > 3) > ''HardwareTest should properly validate test_name format'' FAILED > ''- n/a -'' - should be valid > ./spec/models/hardware_test_spec.rb:34: > > # finally, from console > andy at hst-26-147 (Rails234)->sc > Loading development environment (Rails 2.3.4)>> a = HardwareTest.new(:test_name => ''- n/a -'') > > => #<HardwareTest id: nil, test_name: "- n/a -", code_module_id: nil, > test_description: nil, test_reviewed: false, interactive: false, > loopback: false, factory: false, media: false, test_category_id: nil, > created_at: nil, updated_at: nil, creator_id: nil, updater_id: nil>>> a.valid? > > => true > > Granted the console is in dev, not test, but I still don''t get it. > Literally pulled on my hair on this one. ?I''ve tried altering the > Regex many times - adding the :unless => lambda was my last effort. > > I can''t see how this isn''t passing. > > Thanks for any help > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Craig Demyanovich
2009-Nov-21 18:00 UTC
[rspec-users] [rails] strange regex validation failure
Hi Andy, Nothing jumps out at me at the moment. If you think there might be a difference between dev and test, you could launch script/console with the test env. and try it again on the console. $ script/console test Regards, Craig -- Craig Demyanovich Mutually Human Software http://mutuallyhuman.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091121/50cce8c2/attachment.html>