Merry Testing

- 2008-12-25 15:01:54

Just a few examples of the same test written in a few languages. Its testing setting the date on an object that is created in the tests' setup method already. These fall under the unit testing, rather than full-stack testing.

Testing in ObjC with OCUnit

// Add a date and time
- (void)testSettingDate
{   
    NSDate *theDate = [NSDate date];        

    STAssertNoThrow([calc setDate:theDate], @"Shouldn't raise an exception");
    // And it should match when pulled out as well
    STAssertEqualObjects([calc date], theDate,
                         @"%@ should match %@",
                         [calc date], theDate);

}

Testing in Ruby using RSpec

it "should set the date successfully" do
  the_date = Date.today

  @calc.date = the_date
  # And it should match when pulled out as well
  @calc.date.should == the_date
end

Testing in Ruby using Test::Unit

def test_setting_date
  the_date = Date.today
  @calc.date = the_date
  # And it should match when pulled out as well
  assert_equal(@calc.date, the_date)
end

Testing in PHP using PHPUnit

function testSettingDate()
{
    $date = date();
    $calc->date = $date;
    # And it should match when pulled out as well
    $this->assertEquals($calc->date, $date);
}

1 Comment on Merry Testing

  1. It's pretty funny actually. I 've been playing with Ruby's Unit testing yesterday and today. And you post about TDD today. "Ruby Unit" is sexy. But looking at your RSpec example, RSpec looks even sexier. I can't wait to get to the relevant chapter in Cooper's book :)


About You

Comment