Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing RACCommand on View Model

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...

like image 436
JFoulkes Avatar asked Jun 16 '13 12:06

JFoulkes


1 Answers

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.

like image 122
JFoulkes Avatar answered Nov 12 '22 23:11

JFoulkes