Skip to content

Instantly share code, notes, and snippets.

@orta
Last active August 29, 2015 14:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save orta/d3ea1b2d10e80d757550 to your computer and use it in GitHub Desktop.
Save orta/d3ea1b2d10e80d757550 to your computer and use it in GitHub Desktop.
Mocks & Stubs in a Specta / Expecta world

Getting a Mocta object

NSUserDefaults<Mocta> *defaults = [Mocta objectWithClass:[NSUserDefaults class]];
id< UITableViewDelegate, MoctaStub> delegate = [Mocta objectWithProtocol:@protocol(UITableViewDelegate)];

Expectations

Expectations are ran on dealloc, or at the end of the test case, so I don't have to do it manually.

object.expect( @selector(method:) )
object.expect( @selector(method:) ).matching(BOOL ^(id self, id argument) { return [argument isEqualToNumber:@YES]; } )

object.reject( @selector(method:) )

Stubbing

object.stub(@selector(method:)).with( ^(id self, id argument) { return @YES } );
object.stub(@selector(method:)).with( ^(id self, id argument) { return @"string" } );
object.stub(@selector(method:)).returning(@YES);

object.stub(@selector(method:)).callOriginal().replaceWith( ^{ return @(YES)} );

Class stubbing

MyClass.class.stub(@selector(method:))
MyClass.class.anyInstance().stubs(@selector(foo:)).return(@YES);
@premedios
Copy link

What's mocta?

@orta
Copy link
Author

orta commented Feb 10, 2015

an imaginary Mocking library that's meant to feel like Specta/Expecta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment