我的应用中有多个单身人士,我这样测试。
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());
});
现在,我不希望在每个测试单例的单元测试文件中复制粘贴该代码段。所以我想知道是否有一个解决方案让这段代码只写一次,然后用于多次测试。