我正在做家庭作业,要求我回复一句完整的句子,表明我最喜欢的奶酪。我已经写出了下面的代码,但我不确定在哪里定义cheeseName
。另外,我如何测试代码以查看它是否返回正确的cheeseName
,NSLog()
?
这是我的.m文件
#import "StringCheese.h"
@implementation StringCheese
- (NSString *) favoriteCheeseStringWithCheese:(NSString *)cheeseName {
/* WORK HERE */
return [NSString stringWithFormat:@"My favorite cheese is %@.", cheeseName];
/ My answer is the code above/
}
这是我的另一个.m文件:
#import <XCTest/XCTest.h>
#import "StringCheese.h"
@interface StringCheeseTests : XCTestCase
@property (nonatomic, strong) StringCheese *stringCheese;
@end
@implementation StringCheeseTests
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
self.stringCheese = [[StringCheese alloc] init];
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testThatCheeseFavoritingWorks {
NSString *ricottaString = @"ricotta";
NSString *favoriteCheese = [self.stringCheese favoriteCheeseStringWithCheese:ricottaString];
XCTAssertEqualObjects(favoriteCheese, @"My favorite cheese is ricotta.", @"Incorrect favorite cheese string returned.");
NSString *goatString = @"goat";
favoriteCheese = [self.stringCheese favoriteCheeseStringWithCheese:goatString];
XCTAssertEqualObjects(favoriteCheese, @"My favorite cheese is goat.", @"Incorrect favorite cheese string returned.");
}
编辑:我运行时的结果
12:21:17.116 xctest[13545:2066149] _XCT_testBundleReadyWithProtocolVersion:minimumVersion: reply received
12:21:17.129 xctest[13545:2066149] _IDE_startExecutingTestPlanWithProtocolVersion:16
Test Suite 'Selected tests' started at 2015-11-29 12:21:17.150
Test Suite 'StringCheeseTests' started at 2015-11-29 12:21:17.153
Test Case '-[StringCheeseTests testThatCheeseFavoritingWorks]' started.
Test Case '-[StringCheeseTests testThatCheeseFavoritingWorks]' passed (0.005 seconds).
Test Suite 'StringCheeseTests' passed at 2015-11-29 12:21:17.162.
Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.009) seconds
Test Suite 'Selected tests' passed at 2015-11-29 12:21:17.165.
Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.015) seconds
Test session log:
/var/folders/c0/1phmslcx35ngtg42z44yk75h0000gn/T/com.apple.dt.XCTest-status/Session-2015-11-29_12:21:08-HEws7M.log
Program ended with exit code: 0
答案 0 :(得分:-2)
打印出值add:
NSLog(@"%@", favouriteCheese);
在 - (void)testThatCheeseFavoritingWorks方法结束时看起来像:
- (void)testThatCheeseFavoritingWorks {
NSString *ricottaString = @"ricotta";
NSString *favoriteCheese = [self.stringCheese favoriteCheeseStringWithCheese:ricottaString];
XCTAssertEqualObjects(favoriteCheese, @"My favorite cheese is ricotta.", @"Incorrect favorite cheese string returned.");
NSString *goatString = @"goat";
favoriteCheese = [self.stringCheese favoriteCheeseStringWithCheese:goatString];
XCTAssertEqualObjects(favoriteCheese, @"My favorite cheese is goat.", @"Incorrect favorite cheese string returned.");
NSLog(@"%@", favoriteCheese);
}