GithubHelp home page GithubHelp logo

llreactivematchers's Introduction

LLReactiveMatchers

Build Status

Expecta matchers for ReactiveCocoa

TL;DR

  • LLReactiveMatchers should be able to cover most of the reasons you want to test Signals.
  • One matcher = One Subscription, n Matchers = n Subscriptions
  • Tests that compose on top of Subjects should use LLSignalTestRecorder and expectations should be made on the recorder.

Introduction

ReactiveCocoa is awesome. However, Unit-Testing Signals can be cumbersome. This set of custom matchers for Expecta exists to help make writing tests significantly easier to write and understand. By performing expectations on Signals directly, you will not have to write subscription code to find out about the events they send.

To test that a particular error is received without matcher:

    NSError *expectedError = ...;
    NSError *receivedError = nil;
    [signal subscribeError:^(NSError *error){
        receivedError = error;
    }];
    
    expect(expectedError).to.equal(receivedError);

Can be changed to this:

    NSError *expectedError = ...;
    expect(signal).to.sendError(expectedError);

Matchers will accept a RACSignals as the actual object, subscribing to the Signal in order to receieve it's events. Further expectations using the matchers will result in additional subscriptions. This encourages the usage of Cold Signals, as well as Signals having repeatable results.

In another case you may reason about all of the values that an asynchronous Signal sends. We want to make sure that only the expected values are sent. In other words that the array of values sent by the Signal and our expected array of values are equal:

    NSMutableArray *sentValues = [NSMutableArray array];
    NSArray *expected = @[@0, @1, @2];
    [signal subscribeNext:^(id value) {
        [sentValues addObject:value];
    }];
    expect(hasCompleted).will.equal(expected);

In the original test, a Mutable Array is used to contain all of the values and then tested against. However, in an asynchronous test it is possible that the Signal sends more values after the next value expectation has passed. For this reason, the original test requires an additional expectation that the Signal has completed:

    NSMutableArray *sentValues = [NSMutableArray array];
    NSArray *expected = @[@0, @1, @2];
    BOOL hasCompleted = NO;
    [signal subscribeNext:^(id value) {
        [sentValues addObject:value];
    } completed:^{
        hasCompleted = YES;
    }];
    expect(hasCompleted && [sentValues isEqualToArray:expected]).will.beTruthy();

Which can be changed to:

    expect(expectedValues).will.sendValues(@[@0, @1, @2]);

The Matchers in this library will ensure that dependent conditions such as the completion of a Signal are met. We can therefore reason that no additional values are sent by the Signal and have a higher degree of confidence in our tests.

Asynchonous Testing

Though Synchronous behaviour is common in Signals and readily testable with to/toNot, LLReactiveMatchers works great with the asynchronous will/willNot. The only exception to this rule is asynchronous negation (willNot) on some asynchronous matchers.

    // This will allways pass if signal does not complete as soon as is subscribed to.
    // If the signal completes in 0.2 seconds and the async timeout is 1.0 seconds this will still pass
    expect(signal).willNot.complete();

Since willNot will succeed as soon as the matcher passes for the first time, a Signal that completes asynchronously will pass this test. Instead of using willContinueTo/willNotContinueTo can be used, as these matching methods will wait until the asynchronous timeout before matching.

    // This will allways pass if signal does not complete as soon as is subscribed to.
    // If the signal completes in 0.2 seconds and the async timeout is 1.0 seconds this will fail.
    expect(signal).willNotContinueTo.complete();

Subjects & Replay Subjects

It is quite common to use a RACSubject or RACReplaySubject in place of a RACSignal for testing compound operators where the order in which order in which events are important. For example, the tests for combineLatest use two Subjects to test that the order in which next events sent effects the output of the compound Signal.

    __block RACSubject *subject1 = nil;
    __block RACSubject *subject2 = nil;
    __block RACSignal *combined = nil;

    beforeEach(^{
    	subject1 = [RACSubject subject];
    	subject2 = [RACSubject subject];
    	combined = [RACSignal combineLatest:@[ subject1, subject2 ]];
    });
    
    it(@"should send nexts when either signal sends multiple times", ^{
    	NSMutableArray *results = [NSMutableArray array];
    	[combined subscribeNext:^(id x) {
    		[results addObject:x];
    	}];
	
    	[subject1 sendNext:@"1"];
    	[subject2 sendNext:@"2"];
	
    	[subject1 sendNext:@"3"];
    	[subject2 sendNext:@"4"];
	
    	expect(results[0]).to.equal(RACTuplePack(@"1", @"2"));
    	expect(results[1]).to.equal(RACTuplePack(@"3", @"2"));
    	expect(results[2]).to.equal(RACTuplePack(@"3", @"4"));
    });

As all the matchers subscribe to Signals when the expectation is resolved, none of the events sent via Subjects will get passed through to the Signal composed with combineLatest; the Subjects have sent their events before the composed Signal is subscribed to. It would be wrong to rectifty this with a RACReplaySubject to re-send events on subscription as Replay Subjects are greedy and will send all their accumilated events on subsciption. This will ignore the ordering in which events were sent by subject1 and subject2, essential for the behaviour we wish to test.

The matchers accept LLSignalTestRecorder in place of a Signal. By creating the LLSignalTestRecorder before Subjects send their values, the composed Signal will receive values sent by the Subjects. This is equivalent subscribing to subscribing to the composed Signal with a RACReplaySubject and matching against the Replay Subject.

    it(@"should send nexts when either signal sends multiple times", ^{
        LLSignalTestRecorder *recorder = [LLSignalTestRecorder recordWithSignal:signal];
        
    	[subject1 sendNext:@"1"];
    	[subject2 sendNext:@"2"];

    	[subject1 sendNext:@"3"];
    	[subject2 sendNext:@"4"];
        
    	expect(results).to.sendValue(0, RACTuplePack(@"1", @"2"));
    	expect(results).to.sendValue(1, RACTuplePack(@"3", @"2"));
    	expect(results).to.sendValue(2, RACTuplePack(@"3", @"4"));
    });

LLSignalTestRecorder does provide additional conveniences over RACReplaySubject. You can avoid repeated calls to create a recorder by creating the recorder up front, in a beforeEach block:

    beforeEach(^{
    	subject1 = [RACSubject subject];
    	subject2 = [RACSubject subject];
    	combined = [[RACSignal combineLatest:@[ subject1, subject2 ]] testRecorder];
    });

Subscription Counts

Occasionally, you may need to make assertions about the number of subscriptions to a Signal, for example when describing multicasting behaviour, where repeated side-effects pose a problem. Instead of deriving a new Signal, you can mark a Signal as having it's subscription invocations counted. Use startCountingSubscriptions

    RACSignal *signal = [[RACSignal sideEffectingSignal] startCountingSubscriptions];
    RACSignal *multicastingSignal = [RACSignal multicastingSignalWithSignal:signal];
    
    [multicastingSignal subscribeCompleted:^{}];
    [multicastingSignal subscribeCompleted:^{}];
    [multicastingSignal subscribeCompleted:^{}];
    
    expect(signal).to.beSubscribedTo(1);

Matchers

    expect(signal).to.beSubscribedTo(expectedCount);  //Succeeds if 'signal' sends exactly the number of events of 'expectedCounts'. Fails if startCountingSubscriptions has not been called.
    expect(signal).to.complete();   //Succeeds if 'signal' completes before matching.
    expect(signal).to.error(); //Succeeds if 'signal' errors before matching.
    expect(signal).to.finish(); //Succeeds if 'signal' completes or errors before matching.
    expect(signal).to.matchError(matchBlock); //Succeeds if 'matchBlock' returns YES from 'matchBlock' provided.
    expect(signal).to.matchValue(matchIndex, matchBlock); //Succeeds if 'matchBlock' returns YES from 'matchIndex' provided.
    expect(signal).to.matchValues(matchBlock);  //Succeeds if 'matchBlock' returns YES for all values that 'signal' sends.
    expect(signal).to.sendError(expectedError);  //Succeeds if 'signal' sends an error that is equal to 'expectedError'. 'expectedError' can be an NSError, RACSignal or LLSignalTestRecorder.
    expect(signal).to.sendEvents(expectedEvents);  //Succeeds if 'signal' and 'expectedSignal' send exactly the same events including next values. 'expectedEvents' can be a RACSignal or LLSignalTestRecorder.
    expect(signal).to.sendValue(matchIndex, expectedValue);  //Succeeds if 'signal' sends 'expectedValue' at 'matchIndex'.
    expect(signal).to.sendValues(expectedValues);  //Succeeds if 'signal' exactly sends all of the values in 'expectedValues' and then finishes. 'expectedValues' can be a RACSignal, LLSignalTestRecorder or an NSArray of expected values. 
    expect(signal).to.sendValuesWithCount(expectedCount);  //Succeeds if 'signal' sends exactly the number of events of 'expectedCounts', waits for 'signal' to finish.

Todo

  • Injecting Mock Objects for testing Side-Effects

llreactivematchers's People

Contributors

antol avatar glyuck avatar lawrencelomax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

llreactivematchers's Issues

Too much RAC

This repo contains too much ReactiveCocoa. Use it less.

Super weird behaviour of switchToLatest after installing LLReactiveMatchers

After installing LLReactiveMatchers (via cocoa pods) on unit test target I am getting exception on switchToLatest, which completely doesn't make sense (exception is quite self explanatory). Once I remove LLReactiveMatchers from project, problem disappears.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-switchToLatest requires that the source signal (<RACDynamicSignal: 0x7fcd23c5db40> name: ) send signals. Instead we got: <RACDynamicSignal: 0x7fcd23c86010> name: '

Matchers - haveIdenticalValues()

Matches two Signals that have the same number of next events, in the same order, with all values the same

  • Fails either of the Signals have not finished
  • Succeeds if events are the same, but one Signal ends in error, the other in completion
  • Fails if any of the events do not match

match* matchers are not obvious

From the documentation I cannot understand the concept of matchIndexs. What does that mean?

Further, the use of to.sendValue(matchIndex, value) is not covered outside of the context of multicast.

It's very confusing!

Matchers - haveIdenticalErrors()

Asserts that two Signals have the same Errors.

  • Fails if the Signals do not error
  • Passes if the Signals have errors with the same error value
  • Fails if the Signals have errors with different error values

Semantics for Completion & Error testing Asynchronously

The behaviour of asynchronous matching is to continuously check the matcher passes until the timeout elapses, or the matcher passes.

This can pose a problem for negative matchers, which essentially invert the matcher.

Given an asynchronous signal that will complete before the timeout period, but after the matcher is first polled, it will pass because the condition that completed is not sent will be satisfied immediately be passed. The desired behaviour would be to check that the condition is satisfied for the duration of the timeout period.

// Lets assume that this signal does not exhibit the desired behaviour of not completing
// It will complete a short time after it is subscribed to
RACSignal *signal;
// Passes immediately
expect(signal).toNot.complete();

This knowledge about the expectation would probably need to be implemented in something like EXPExpect

Trawl the ReactiveCocoa repo for common assertions

There are a bunch of different behaviours that the ReactiveCocoa repo checks in Unit Tests other than checking values sent. I've seen some tests on the number of subscriptions that occur and disposal.

I might take a look at rewriting some of the RAC Unit tests using LLReactiveMatchers, but I want to prevent this from being anything other than an experiment to figure out how suitable LLReactiveMatchers is to the task.

Matcher - beSubscribedTo(count)

A matcher for the number of subscriptions a Signal takes, important for testing side-effects for Signals that perform side-effects on Subscription.

This will probably not be orthogonal to the changes made to LLSignalTestRecorder, which now has multicasting behaviour. Ideally the monitoring of subscription counts would occur inside the recorder so as to avoid swizzling subscribe: on RACSignal

Making LLSignalTestRecorder a Signal

LLSignalTestRecorder is essentially a RACReplaySubject that exposes all the values it has received as properties. During #13, it seemed to me that LLSignalTestRecorder maybe better off as a Signal, then usage of LLSignalTestRecorder as an actual doesn't need to be special cased. The behaviour of LLSignalTestRecorder will then be much clearer to newcomers to the library

Double inclusion when Used as a Cocoapods

There is currently an issue with CocoaPods that means ReactiveCocoa will get linked twice when LLReactiveMatchers is used as a Pod.

Since this project should be vendable as a CocoaPod, I will watch and then close this issue when the issue has been fixed.

Optimisations - Fail early in appropriate matchers

In matchers for next values we can fail early

For example with haveIdenticalValues() for two signals:
1, 2, 3, 4, 5
1, 2, 5, 4, 5

we can fail when the events are known to be non-identical, i.e at index 2 of the values

Matchers - completes()

A matcher that tests whether a Signal ends in a completion event

  • Fails if the Signal has not yet completed
  • Fails if the Signal ends in error
  • Succeeds if the Signal has sent a completed event

Matchers - sendsError()

A matcher that tests that a Signal finishes with a specific error

  • Fails if the Signal has not finished
  • Fails if the Signal does not send the error provided
  • Succeeds if the expected error is equal to the one sent by the Signal.

Matchers - haveIdenticalEnd()

Matches two Signals that end in the same way effectively completes() || sendsError()

Matches:

  • Two Signals that both end in an error
  • Two Signals that end in the same error

Does Not Match:

  • Two Signals that end in different errors
  • One Signal ending in error, the other in completion

Temporally sensitive Signals require subscription before Subject values are sent

I was changing a bunch of the Unit Tests in the RAC repo to see how suitable the matchers were. By using RACReplaySubjects in place of subjects, Signal chains can be tested at the end of the case

So this:

    NSMutableArray *values = [NSMutableArray array];
    [merged subscribeNext:^(id x) {
        [values addObject:x];
    }];

    [sub1 sendNext:@1];
    [sub2 sendNext:@2];
    [sub2 sendNext:@3];
    [sub1 sendNext:@4];

    NSArray *expected = @[ @1, @2, @3, @4 ];
    expect(values).to.equal(expected);

Goes to this:

    [sub1 sendNext:@1];
    [sub2 sendNext:@2];
    [sub2 sendNext:@3];
    [sub1 sendNext:@4];

    NSArray *expected = @[ @1, @2, @3, @4 ];
    expect(merged).to.sendValues(expected);

However this will not work where ordering of the values is significant as is the case in the tests for merge:

    NSMutableArray *results = [NSMutableArray array];
    [combined subscribeNext:^(id x) {
        [results addObject:x];
    }];

    [subject1 sendNext:@"1"];
    [subject2 sendNext:@"2"];

    [subject1 sendNext:@"3"];
    [subject2 sendNext:@"4"];

    expect(results[0]).to.equal(RACTuplePack(@"1", @"2"));
    expect(results[1]).to.equal(RACTuplePack(@"3", @"2"));
    expect(results[2]).to.equal(RACTuplePack(@"3", @"4"));

The alternative will fail:

    LLSignalTestRecorder *recorder = [LLSignalTestRecorder recordWithSignal:combined];

    [subject1 sendNext:@"1"];
    [subject2 sendNext:@"2"];

    [subject1 sendNext:@"3"];
    [subject2 sendNext:@"4"];

    expect(recorder).to.sendValuesIdentically(@[ RACTuplePack(@"1", @"2"), RACTuplePack(@"3", @"2"), RACTuplePack(@"3", @"4")]);

This is because the each of the two subjects are unaware (by design) of the order that they were called relative to one another. By passing a LLSignalTestRecorder as the actual instead of a Signal, we can remove the dependency on a RACReplaySubject and keep the significance of the ordering.

In particular this will apply for tests that use Subjects because their values are sent at the time sendNext: is called rather than when they are subscribed to in the matcher.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.