mardi 25 août 2015

How to make OCMock remember provided arguments?

A have a model class which has a delegate:

@protocol ModelClassDelegate
@optional
- (void)modelClass:(ModelClass*)modelClass wantsToShowAlertView:(UIAlertView*)alertView;
@end

@interface ModelClass : NSObject
@property (weak, nonatomic) id<ModelClassDelegate> delegate;
@end

In my test I wish to get the provided alert view and verify its fields. For now I just made a stub class called ModelClassDelegateStub which implements all the methods of delegate and remembers provided arguments, so I can do like this:

// when
[sut someAction];

// then
expect(delegate.selector).equal(@selector(modelClass:wantsToShowAlertView:));
UIAlertView *alertView = delegate.alertView;
expect([alertView numberOfButtons]).equal(2);
expect([alertView title]).equal(NSLocalizedString(@"TITLE", @""));
expect([alertView message]).equal(NSLocalizedString(@"MESSAGE", @""));
expect([alertView buttonTitleAtIndex:0]).equal(NSLocalizedString(@"No", @""));
expect([alertView buttonTitleAtIndex:1]).equal(NSLocalizedString(@"Yes", @""));
expect([alertView delegate]).equal(sut);

Is it possible to do the same thing with OCMock or other Mock framework?

Aucun commentaire:

Enregistrer un commentaire