Robert J. Clegg
Robert J. Clegg

Reputation: 7360

Testing RACSignals with XCTest

I'm using the MVVM paradigm in my current iOS app. Recently, I have also started using ReactiveCocoa with the project. I've now moved onto experimenting with Unit testing as well.

The problem I am facing is how to correctly test the custom RACSignals I have created. Here is an example of a test signal I am testing. This signal is used with a UItextField and will stop unwanted characters being entered into the textField. In this case, I am only allowing numbers:

//Declared like so:
-(RACSignal *)onlyAllowNumbersforTextFieldSignal:(RACSignal *)signal

//used like this: 
 RAC(testTextField, text) = [self.viewModel onlyAllowNumbersforTextFieldSignal:testTextField.rac_textSignal];

Now the signal works perfectly in the viewModel and in the viewController - I now just want to create a test case for these sorts of signals.

Upvotes: 7

Views: 382

Answers (1)

Michał Ciuba
Michał Ciuba

Reputation: 7944

You can use +[RACSignal return:] method to provide an input signal (instead of the text field's one). Then use -first method to get the value of the output signal from the view model:

- (void)testExample {
  RACSignal *textSignal = [RACSignal return:@"a123"];
  //assuming that you initialized self.viewModel in setUp method of your test case
  NSString *result = [[self.viewModel onlyAllowNumbersforTextFieldSignal:textSignal] first];
  XCTAssertEqualObjects(result, @"123");
}

Upvotes: 1

Related Questions