Search code examples
objective-cocunit

Why is this first OCUnit unit test passing?


It should fail, but it's passing. So I assume my syntax is wrong somewhere (although it builds).

#import <Foundation/Foundation.h>

@interface FirstCocoaLibrary : NSObject

-(int)AddFirstNum:(int) i1 withSecondNum:(int) i2;

@end


#import "FirstCocoaLibrary.h"

@implementation FirstCocoaLibrary

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (int)AddFirstNum:(int) i1 withSecondNum:(int) i2{

    return i1 + i2;
}

@end

#import <SenTestingKit/SenTestingKit.h>
#import "FirstCocoaLibrary.h"

@interface FirstCocoaLibraryTests : SenTestCase{
    FirstCocoaLibrary *_adder;
}

@end

#import "FirstCocoaLibraryTests.h"

@implementation FirstCocoaLibraryTests

- (void)setUp
{
    // Set-up code here.
    _adder = [[FirstCocoaLibrary alloc] init];
}

- (void)tearDown
{
    // Tear-down code here.

}

- (void)testExample
{
    //STFail(@"Unit tests are not implemented yet in FirstCocoaLibraryTests");
}

- (void)ShouldAdd2Numbers {

    int result = [_adder AddFirstNum:4 withSecondNum:5];
    STAssertEquals(0, result, @"Test Failed at adding", result);
}

@end

Solution

  • OCUnit only automatically runs methods whose names start with test. I don't think it's passing; it's not getting run at all. Rename ShouldAdd2Numbers to testAddFirstNumWithSecondNum.

    Also note that Obj-C convention is for method names, and each section of a method name, to start with a lower-case letter. Thus, addFirstNum:withSecondNum: