I'm trying to test the result of a RACCommand executing on my view model.
I set my submit command up like so:
- (void) createSubmitCommand
{
@weakify(self);
self.submitCommand = [RACCommand commandWithCanExecuteSignal: [self validSignal]];
self.submitSignal = [self.submitCommand
addSignalBlock:^RACSignal *(id value) {
@strongify(self);
return [self save];
}];
}
- (RACSignal *) save
{
RACSubject *saveSubject = [RACSubject subject];
[self.model.managedObjectContext MR_saveOnlySelfWithCompletion:^(BOOL success, NSError *error) {
if (!success)
{
[saveSubject sendError: error];
}
else
{
[saveSubject sendNext: nil];
[saveSubject sendCompleted];
}
}];
return saveSubject;
}
createSubmitCommand is called when I init my view model and validSignal is valid in the test context.
I'm using MagicalRecord for Core Data persistance and Kiwi for testing. I need to test that when I call [[viewModel submitCommand] execute: nil] that my model is saving.
My test looks something like this:
__block NSArray *models = nil;
[[vm submitSignal] subscribeNext:^(id x) {
models = [Model MR_findAll];
}];
[[vm submitCommand] execute: nil];
[[expectFutureValue(models) should] haveCountOf: 2];
The issue is that save is asynchronous and doesn't block then the test finishes and tears down my NSManagedObjectContext and the test fails. I feel that I've got the test completely wrong for what I'm trying to do or I'm misusing RACCommand but I don't know which...
Turns out this was me being stupid. My expect for this test should have been:
[[expectFutureValue(models) shouldEventually] haveCountOf: 2];
Kiwi seems to stick around and wait for the result now.
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