Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 746
Robert J. Clegg Avatar asked Aug 20 '15 20:08

Robert J. Clegg


1 Answers

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");
}
like image 109
Michał Ciuba Avatar answered Nov 18 '22 09:11

Michał Ciuba