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 RACSignal
s 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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With