Unit-testing a simple usage of RACSignal with RACSubject

给你一囗甜甜゛ 提交于 2019-12-03 21:51:32

This may not answer your question exactly, but it may give you insights on how to effectively test your signals. I've used 2 approaches myself so far:

XCTestCase and TRVSMonitor

TRVSMonitor is a small utility which will pause the current thread for you while you run your assertions. For example:

TRVSMonitor *monitor = [TRVSMonitor monitor];

[[[self.service searchPodcastsWithTerm:@"security now"] collect] subscribeNext:^(NSArray *results) {
    XCTAssertTrue([results count] > 0, @"Results count should be > 0";
    [monitor signal];

} error:^(NSError *error) {
    XCTFail(@"%@", error);
    [monitor signal];
}];

[monitor wait];

As you can see, I'm telling the monitor to wait right after I subscribe and signal it to stop waiting at the end of subscribeNext and error blocks to make it continue executing (so other tests can run too). This approach has the benefit of not relying on a static timeout, so your code can run as long as it needs to.

Using CocoaPods, you can easily add TRVSMonitor to your project:

pod "TRVSMonitor", "~> 0.0.3"

Specta & Expecta

Specta is a BDD/TDD (behavior driven/test driven) test framework. Expecta is a framework which provides more convenient assertion matchers. It has built-in support for async tests. It enables you to write more descriptive tests with ReactiveCocoa, like so:

it(@"should return a valid image, with cache state 'new'", ^AsyncBlock {
    [[cache imageForURL:[NSURL URLWithString:SECURITY_NOW_ARTWORK_URL]] subscribeNext:^(UIImage *image) {
        expect(image).notTo.beNil();
        expect(image.cacheState).to.equal(JPImageCacheStateNew);

    } error:^(NSError *error) {
        XCTFail(@"%@", error);

    } completed:^{
        done();
    }];
});

Note the use of ^AsyncBlock {. Using simply ^ { would imply a synchronous test.

Here you call the done() function to signal the end of an asynchronous test. I believe Specta uses a 10 second timeout internally.

Using CocoaPods, you can easily add Expecta & Specta:

pod "Expecta", "~> 0.2.3"
pod "Specta", "~> 0.2.1"
drhr

See this question: https://stackoverflow.com/a/19127547/420594

The XCAsyncTestCase has some extra functionality to allow for asynchronous test cases.

Also, I haven't looked at it in depth yet, but could ReactiveCocoaTests be of some interest to you? On a glance, they appear to be using Expecta.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!