# First Test Failures: Anything Obvious?

**URL:** <https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403>\
**Category:** Rails\
**Created:** [August 17, 2010, 6:18am UTC](https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403 "2010-08-17T06:18:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 17, 2010, 6:18am UTC](https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403/1 "2010-08-17T06:18:46Z")

</div>

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]:  
 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.

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 17, 2010, 7:32am UTC](https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403/2 "2010-08-17T07:32:17Z")

</div>

Try to save the object first and then check for validity.

2010/8/17 Terry M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 17, 2010, 9:33am UTC](https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403/3 "2010-08-17T09:33:28Z")

</div>

On 17 August 2010 05:18, Terry M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> assert recording.errors[:title].any?  
> [/test/unit/recording\_test.rb:6]:  
> 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

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 18, 2010, 7:12am UTC](https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403/4 "2010-08-18T07:12:55Z")

</div>

Colin L. wrote:

> On 17 August 2010 06:31, Gintautas Å imkus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

The 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.

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 17, 2010, 9:34am UTC](https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403/5 "2010-08-17T09:34:03Z")

</div>

On 17 August 2010 06:31, Gintautas Å imkus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 18, 2010, 7:37am UTC](https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403/6 "2010-08-18T07:37:12Z")

</div>

Terry M. 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)

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 18, 2010, 7:42am UTC](https://www.ruby-forum.com/t/first-test-failures-anything-obvious/193403/7 "2010-08-18T07:42:37Z")

</div>

Terry M. wrote:

> Terry M. 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
