ios – XCTest断言期望未得到满足

ios – XCTest断言期望未得到满足,第1张

概述当使用XCTest和XCTestExpectation编写某个异步测试时,我想声明某个块没有被执行.以下代码成功断言已执行块,如果不是,则测试失败. #import <XCTest/XCTest.h>#import "Example.h"@interface Example_Test : XCTestCase@property (nonatomic) Example *example; 当使用XCTest和XCTestExpectation编写某个异步测试时,我想声明某个块没有被执行.以下代码成功断言已执行块,如果不是,则测试失败.

#import <XCTest/XCTest.h>#import "Example.h"@interface Example_Test : XCTestCase@property (nonatomic) Example *example;@end@implementation Example_Test- (voID)setUp {    [super setUp];}- (voID)tearDown {     [super tearDown];}- (voID)testExampleWithCompletion {    self.example = [[Example alloc] init];    XCTestExpectation *expectation = [self expectationWithDescription:@"expection needs to be fulfilled"];    [self.example exampleWithCompletion:^{        [expectation fulfill]    }];    [self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {        if (error) {            NSLog(@"Timeout Error: %@",error);        }    }];}

似乎没有一种明显的方法可以反过来执行此 *** 作;如果块在超时后没有执行则测试成功,如果在超时之前执行则失败.除此之外,我想声明该块在以后满足不同条件时执行.

有没有一种直接的方法来使用XCTestExpectation执行此 *** 作,还是我必须创建一个解决方法?

解决方法 我知道这已经有几年了,但我偶然发现XCTestExpectation上的一个参数让你反转了期望值.希望这将有助于其他人绊倒这个.答案是在Swift中

let expectation = XCTestExpectation(description: "")expectation.isInverted = true

文档:https://developer.apple.com/documentation/xctest/xctestexpectation/2806573-isinverted

总结

以上是内存溢出为你收集整理的ios – XCTest断言期望未得到满足全部内容,希望文章能够帮你解决ios – XCTest断言期望未得到满足所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1031391.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存