在编写单元测试时不要重复自己

时间:2014-06-23 09:52:17

标签: objective-c unit-testing dry

我的应用中有多个单身人士,我这样测试。

SPSchemeParser* (^createUniqueInstance) (void) = ^SPSchemeParser* (void) {
    return [[SPSchemeParser alloc] init];
};

SPSchemeParser* (^getSharedManager) (void) = ^SPSchemeParser* (void) {
    return [SPSchemeParser sharedManager];
};

// Test singleton

it(@"can create a unique instance", ^{
    expect(createUniqueInstance()).toNot.beNil();
});

it(@"can create a shared instance", ^{
    expect(sut).toNot.beNil();
});

it(@"returns always the same shared instance", ^{
    expect(sut).to.equal(getSharedManager());
});

it(@"returns different unique instance", ^{
    expect(createUniqueInstance()).toNot.equal(createUniqueInstance());
});

it(@"returns a unique instance different to the shared instance", ^{
    expect(sut).toNot.equal(createUniqueInstance());
});

现在,我不希望在每个测试单例的单元测试文件中复制粘贴该代码段。所以我想知道是否有一个解决方案让这段代码只写一次,然后用于多次测试。

0 个答案:

没有答案