Still following my rails book (more like, adapting to my project as I go). My first attempt at a unit test failed. Adapting the example in the book, I came up with this: require ''test_helper'' class RecordingTest < ActiveSupport::TestCase test "record attributes must not be empty" do recording = Recording.new assert recording.invalid? assert recording.errors[:title].any? assert recording.errors[:speaker].any? assert recording.errors[:date_of_event].any? assert recording.errors[:file].any? end end Which gives me: 1) Failure: test_record_attributes_must_not_be_empty(RecordingTest) [/test/unit/recording_test.rb:6]: <false> is not true. 1 tests, 1 assertions, 1 failures, 0 errors rake aborted! Command failed with status (1): [/usr/bin/ruby -I"lib:test" "/usr/lib64/rub...] The idea was to test my validation code from the model: # cat app/models/recording.rb class Recording < ActiveRecord::Base validate :title, :speaker, :date_of_event, :file, :presence => true validate :title, :file, :uniqueness => true end From the little I understand, I''m not sure why the record object is not showing up as invalid as it should, having no data added yet. The only thing I can note is that I''m using rails 2.3.5, whereas I think the book is meant for 3. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Try to save the object first and then check for validity. 2010/8/17 Terry Michaels <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Still following my rails book (more like, adapting to my project as I > go). My first attempt at a unit test failed. Adapting the example in the > book, I came up with this: > > require ''test_helper'' > > class RecordingTest < ActiveSupport::TestCase > test "record attributes must not be empty" do > recording = Recording.new > assert recording.invalid? > assert recording.errors[:title].any? > assert recording.errors[:speaker].any? > assert recording.errors[:date_of_event].any? > assert recording.errors[:file].any? > end > end > > Which gives me: > > 1) Failure: > test_record_attributes_must_not_be_empty(RecordingTest) > [/test/unit/recording_test.rb:6]: > <false> is not true. > > 1 tests, 1 assertions, 1 failures, 0 errors > rake aborted! > Command failed with status (1): [/usr/bin/ruby -I"lib:test" > "/usr/lib64/rub...] > > The idea was to test my validation code from the model: > > # cat app/models/recording.rb > class Recording < ActiveRecord::Base > validate :title, :speaker, :date_of_event, :file, :presence => true > validate :title, :file, :uniqueness => true > end > > From the little I understand, I''m not sure why the record object is not > showing up as invalid as it should, having no data added yet. The only > thing I can note is that I''m using rails 2.3.5, whereas I think the book > is meant for 3. > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 August 2010 05:18, Terry Michaels <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Still following my rails book (more like, adapting to my project as I > go). My first attempt at a unit test failed. Adapting the example in the > book, I came up with this: > > require ''test_helper'' > > class RecordingTest < ActiveSupport::TestCase > test "record attributes must not be empty" do > recording = Recording.new > assert recording.invalid? > assert recording.errors[:title].any? > assert recording.errors[:speaker].any? > assert recording.errors[:date_of_event].any? > assert recording.errors[:file].any? > end > end > > Which gives me: > > 1) Failure: > test_record_attributes_must_not_be_empty(RecordingTest) > [/test/unit/recording_test.rb:6]: > <false> is not true.Have a go in the ruby console. In there you can do recording = Recording.new and then inspect the result, call valid on it and so on to see what is not going as expected. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 August 2010 06:31, Gintautas Šimkus <dihitales-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try to save the object first and then check for validity.I don''t think that is right, for a start it will not save if it is invalid. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> On 17 August 2010 06:31, Gintautas Šimkus <dihitales-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Try to save the object first and then check for validity. > > I don''t think that is right, for a start it will not save if it is > invalid. > > ColinThe point of the unit test is make sure the object /is/ invalid. That is, the object is not supposed to have any data yet, meaning that it will not pass the validation code which requires certain data fields to be filled. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Terry Michaels wrote:> # cat app/models/recording.rb > class Recording < ActiveRecord::Base > validate :title, :speaker, :date_of_event, :file, :presence => true > validate :title, :file, :uniqueness => true > endWait, double checked the book... the method is supposed to be "validates", not "validate". Problem solved? Unfortunately, not quite. New unit test run gives me: /usr/bin/ruby -I"lib:test" "/usr/lib64/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/helpers/home_helper_test.rb" "test/unit/helpers/recordings_helper_test.rb" "test/unit/recording_test.rb" /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1959:in `method_missing'': undefined method `validates'' for #<Class:0x7fefd5c73478> (NoMethodError) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Terry Michaels wrote:> Terry Michaels wrote: >> # cat app/models/recording.rb >> class Recording < ActiveRecord::Base >> validate :title, :speaker, :date_of_event, :file, :presence => true >> validate :title, :file, :uniqueness => true >> end > > Wait, double checked the book... the method is supposed to be > "validates", not "validate". > > Problem solved? Unfortunately, not quite. New unit test run gives me: > > /usr/bin/ruby -I"lib:test" > "/usr/lib64/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" > "test/unit/helpers/home_helper_test.rb" > "test/unit/helpers/recordings_helper_test.rb" > "test/unit/recording_test.rb" > /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1959:in > `method_missing'': undefined method `validates'' for > #<Class:0x7fefd5c73478> (NoMethodError)Found a different API on the Web. Not like what is in the book, but it works. Changed the model code to the following, and then all the tests pass fine: class Recording < ActiveRecord::Base validates_presence_of :title validates_presence_of :speaker validates_presence_of :date_of_event validates_presence_of :file validates_presence_of :presence validates_uniqueness_of :title validates_uniqueness_of :file end -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.