Forum: Ruby on Rails A unit test that should pass

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9c0eebc506c56c64dc616cdf123fa671?d=identicon&s=25 Chris Chris (aboulafia)
on 2006-03-26 01:44
Hi, I wonder why this unit test fails.

The model :
class Article < ActiveRecord::Base
  set_table_name "publish_articles"
  belongs_to :category

  validates_presence_of :title, :excerpt
  #snip
end

The test :
def test_validate
    @article.title = nil
    @article.excerpt = nil
    assert !@article.save
    assert_equal 2, !@article.errors.count
end

!@article.errors.count returns 'false' instead of 2
I tried also with @article.title = ''

Any idea ?
5301cef77ec4942463fae0ba820b6b57?d=identicon&s=25 Tim Case (Guest)
on 2006-03-26 01:53
(Received via mailing list)
try changing

assert_equal 2, !@article.errors.count

to be

assert_equal 2, @article.errors.count

The bang(!) means not and when you place it in front of !@
article.errors.count it's returning the opposite of whether there is an
@
articles.errors.count value returned, which in this case is false.
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-03-26 01:54
(Received via mailing list)
On 3/25/06, Christophe Gimenez <kandjidev@yahoo.fr> wrote:
>
>
Shouldn't that be?:
assert_equal 2, @article.errors.count
2c80e1b0837eec76d38d4f7fa1c15e87?d=identicon&s=25 Michael Gorsuch (Guest)
on 2006-03-26 01:54
(Received via mailing list)
Where is "@article" getting created?

Also, what if you put your validation statements in the model on two
seperate lines?
9c0eebc506c56c64dc616cdf123fa671?d=identicon&s=25 Chris Chris (aboulafia)
on 2006-03-26 01:58
Hooooo yes, I didn't see the typo !

Should go to bed now, thanks you all !
This topic is locked and can not be replied to.