The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby Asserting Equality with True

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jay Fields

Posts: 765
Nickname: jayfields
Registered: Sep, 2006

Jay Fields is a software developer for ThoughtWorks
Ruby Asserting Equality with True Posted: Sep 8, 2006 2:32 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jay Fields.
Original Post: Ruby Asserting Equality with True
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jay Fields
Latest Posts From Jay Fields Thoughts

Advertisement
I believe Bill Caputo wrote a blog entry (can't find the link, sorry) several months ago stating that he preferred tests that assert equality over tests that simply assert truth. If I remember correctly the main point was that an error message such as 1 expected but was 2 is much more descriptive than false is not true.

I agree with Bill and think it's a great guideline to follow when writing tests. Unfortunately, there are also times when I need to assert truth. These tests usually appear when I need to test a method that should return true or false. In these situations I've found it valuable to use assert_equal instead of assert when programming in Ruby.

The largest reason for this choice is because Ruby treats everything that is not nil or false as true. Therefore, if you are testing the coded? method and you have an implementation error such as:
def coded?
state = :coded
end
Your test will still pass if the test is coded as:
def test_coded?
assert obj.coded?
end
However, this test will fail:
def test_coded?
assert_equal true, obj.coded?
end
Clearly, you should also have a test that verifies that coded? is false; thus, better coverage mitigates this risk. However, I still prefer the assert_equal test implementation because I feel it makes my test suite more robust.

Read: Ruby Asserting Equality with True

Topic: Test Driven Development and Teenage Sex Previous Topic   Next Topic Topic: Upcoming Ruby on Rails Books (E-Commerce, Nutshell and More!)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use