8

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Robert J. Clegg
  • 7,231
  • 9
  • 47
  • 99

1 Answers1

1

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");
}
Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
  • Thank you so much for this. I will give it ago as soon as I get some time to carry on with test cases. If only I could have awarded you the bounty.! Will also update once I have tested this fully. – Robert J. Clegg Sep 01 '15 at 16:32