我是 Objective-C 的新手,我试图立即开始使用 TestDrivenDevelopment,因为我发现它确实令人放心,至少测试确实通过了。
在此之前,我用 Java 制作了一些教程,其中我对 TDD 有了一些了解。 Brett Schucherts Video-Tutorials他一步步编写完整的 RPNCalculator 是一个金矿,通过观察他的实际操作来学习这些东西。
为了减少代码重复,您可以这样做:
@Before
public void init() {
/* Stuff that which will be set up before the call of each test method/*
}
然后在 Java 的 TestClass 中的每个测试之前调用它。
这是我想在 Objective-C + Xcode 中实现的。我应该提到,我正在使用 Xcode 4.3(最新版本)并且我正在使用内置的 TestFramework。
我在网上找到的唯一接近我正在寻找的东西是 this answer on SO .
不幸的是,我无法重现所描述的做事方式。一个最小的可运行示例和/或更详细的解释对于新人来说将是非常棒的并且非常感激!
顺便说一句,抱歉英语不好。还在学习语言。 :-)
编辑:
这是一个不起作用的最小示例。也许有人可以告诉我出了什么问题。 Xcode 似乎无法识别测试方法主体内的 board
。
#import <SenTestingKit/SenTestingKit.h>
@interface ABoardShould : SenTestCase
@end
#import "ABoardShould.h"
@implementation ABoardShould
- (void)setUp
{
[super setUp];
int rowCount = 6;
int columnCount = 7;
Board *board = [[Board alloc] initWithShapeRowCount:rowCount andColumnCount:columnCount];
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
- (void)testHaveItsShapeSetWhenInitialised {
STAssertEquals([board rowCount], rowCount, @"");
STAssertEquals([board columnCount], columnCount, @"");
}
- (void)testHaveTheDimensionsOfItsBoardMatchTheGivenShape {
NSMutableArray *expectedFields = [[NSMutableArray alloc] initWithCapacity:columnCount*rowCount];
for(int i=0; i < (rowCount*columnCount); i++) [expectedFields addObject: [NSNumber numberWithInt: 0]];
STAssertEquals([expectedFields count], [[board fields] count], @"");
}
最佳答案
测试用例类的设置和拆卸方法必须调用setUp
和tearDown
。 Xcode Unit Testing Guide: Writing Test Case Methods 中对此进行了描述。 .
在您的示例中,board
是 setUp
方法中的局部变量。您需要将其设为实例变量。
关于objective-c - 如何在 Objective-C Xcode 包中的每个单元测试之前实现 setup() 和teardown()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10369406/