gpt4 book ai didi

ios - 助手类中的断言被忽略

转载 作者:行者123 更新时间:2023-12-01 17:58:28 26 4
gpt4 key购买 nike

我有一个测试用例和一个助手类。在辅助类中,我也想使用断言,如下所示:

MainTests.h

#import <SenTestingKit/SenTestingKit.h>

@interface MainTests : SenTestCase

@end

MainTests.m
#import "MainTests.h"
#import "HelperClass.h"

@implementation MainTests

- (void)testExample {
HelperClass *helperClass = [[HelperClass alloc] init];
[helperClass fail];
}

@end

HelperClass.h
#import <SenTestingKit/SenTestingKit.h>

@interface HelperClass : SenTestCase

- (void)fail;

@end

HelperClass.m
#import "HelperClass.h"

@implementation HelperClass

- (void)fail {
STFail(@"This should fail");
}

@end

旁注:我必须使helper类成为 SenTestCase的子类,才能访问断言宏。

会忽略帮助程序类中的断言。有什么想法吗?如何在助手类中使用断言?

最佳答案

我今天也遇到了同样的问题,并提出了一种适用于我的目的的黑客工具。进入SenTestCase宏,我注意到它们在辅助程序上调用[self ...],但没有触发断言。因此,将源类连接到帮助程序就可以为我工作。对您的问题类别的更改如下所示:

MainTests.h

#import <SenTestingKit/SenTestingKit.h>

@interface MainTests : SenTestCase

@end

MainTests.m
#import "MainTests.h"
#import "HelperClass.h"

@implementation MainTests

- (void)testExample {
// Changed init call to pass self to helper
HelperClass *helperClass = [[HelperClass alloc] initFrom:self];
[helperClass fail];
}

@end

HelperClass.h
#import <SenTestingKit/SenTestingKit.h>

@interface HelperClass : SenTestCase

- (id)initFrom:(SenTestCase *)elsewhere;
- (void)fail;

@property (nonatomic, strong) SenTestCase* from;

@end

HelperClass.m
#import "HelperClass.h"

@implementation HelperClass

@synthesize from;

- (id)initFrom:(SenTestCase *)elsewhere
{
self = [super init];
if (self) {
self.from = elsewhere;
}
return self;
}

- (void)fail {
STFail(@"This should fail");
}

// Override failWithException: to use the source test and not self
- (void) failWithException:(NSException *) anException {
[self.from failWithException:anException];
}

@end

完全有可能需要更多的替代来实现更高级的功能,但这对我来说是成功的窍门。

关于ios - 助手类中的断言被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13495671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com