Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing a class method in XCTestCase

I am currently beginning writing some unit test in my project. However, seems that I met some issue:

- (void)testThatItIsValidNumber
{
    BOOL result1 = [NSString isValidNumber:@"1234567890987654"];
    BOOL result2 = [NSString isValidNumber:@" ^123dadaj"];
    BOOL result3 = [NSString isValidNumber:@"123 09 123"];
    BOOL result4 = [NSString isValidNumber:@"~#)(^%@*#&Bhadahj1@"];

    XCTAssertFalse(result1);
    XCTAssert(result2);
    XCTAssertFalse(result3);
    XCTAssert(result4);
}

This gave me an error:

caught "NSInvalidArgumentException", "+[NSString isValidNumber]: unrecognized selector sent to class 0x1069448e0"

Any one can guide me to the right way?

Edit:

+ (BOOL)isValidNumber:(NSString *)value {
    const char *cvalue = [value UTF8String];
    unsigned long len = strlen(cvalue);
    for (int i = 0; i < len; i++) {
        if(!isNumber(cvalue[i])){
            return FALSE;
        }
    }
    return TRUE;
}
like image 753
Zigii Wong Avatar asked Dec 06 '25 03:12

Zigii Wong


1 Answers

Make sure that your category is added to your test target otherwise it won't be available in your tests.

To do that, just select your class from the file navigator, and press the document inspector on the right side. In the Target membership section, check your test target.

like image 191
BalestraPatrick Avatar answered Dec 07 '25 18:12

BalestraPatrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!