In attempting to use XCTest to test my application, I get a build error when doing the following:
#import <XCTest/XCTest.h>
@interface MyTests : XCTestCase
@end
@implementation MyTests
- (void)testExample
{
NSString *str = @"foo";
XCTAssertTrue(YES, str); // Parse issue: Expected ')'
}
@end
but I do not get a build error if I do this:
#import <XCTest/XCTest.h>
@interface MyTests : XCTestCase
@end
@implementation MyTests
- (void)testExample
{
XCTAssertTrue(YES, @"foo"); // this is just fine...
}
@end
The build error that I get is:
Parse issue: Expected ')'
and it puts an arrow under the "s" in "str".
I discovered that I can remedy this by changing
XCTAssertTrue(YES, str)
to
XCTAssertTrue(YES, @"%@", str)
but I just cannot figure out why that makes a difference. Can somebody please explain why this is so?
The XCT...
macros are written to accept format strings — the strings themselves are optional (so that writing XCTAssertTrue(YES)
is valid), but they must be constant strings. You cannot pass objects into the macro without having a format string, which is why XCTAssertTrue(YES, @"%@", str)
works, but, say, XCTAssertTrue(YES, str)
or XCTAssertTrue(NO, nil)
wouldn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With