Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>OCMock 2</h1> <pre><code>id mock = [OCMockObject mockForClass:[ExtClass class]]; // We stub someMethod BOOL returnedValue = YES; [[[mock stub] andReturnValue:OCMOCK_VALUE(returnedValue)] someMethod]; // Here we stub the alloc class method ** [[[mock stub] andReturn:mock] alloc]; // And we stub initWithParam: passing the param we will pass to the method to test NSString *param = @"someParam"; [[[mock stub] andReturn:mock] initWithParam:param]; // Here we call the method to test and we would do an assertion of its returned value... [YourClassToTest foo:param]; </code></pre> <h1>OCMock3</h1> <pre><code>// Parameter NSURL *url = [NSURL URLWithString:@"http://testURL.com"]; // Set up the class to mock `alloc` and `init...` id mockController = OCMClassMock([WebAuthViewController class]); OCMStub([mockController alloc]).andReturn(mockController); OCMStub([mockController initWithAuthenticationToken:OCMOCK_ANY authConfig:OCMOCK_ANY]).andReturn(mockController); // Expect the method that needs to be called correctly OCMExpect([mockController handleAuthResponseWithURL:url]); // Call the method which does the work [self.myClassInstance authStarted]; OCMVerifyAll(mockController); </code></pre> <h1>Notes</h1> <p>Ensure that in both cases you stub <em>two</em> methods (<code>alloc</code> and the <code>init...</code> method). Also, make sure that both stubbing calls are made on the <em>instance</em> of the class mock (not the class itself).</p> <p>Docs: <strong>Class methods</strong> section in the <a href="http://www.ocmock.org/features/" rel="noreferrer">OCMock features</a></p> <h1>Alternatives</h1> <p>This (strange)solution may be useful in case you want to test legacy code that due to whatever reason you cannot refactor. However, if you can modify the code you should refactor it and get an <code>ExtClass</code> object as a parameter, not a string, delegating the creation of <code>ExtClass</code> out of that method. Your production and test code would be simpler and clearer, specially in a more complex real life case, not in this simple example. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload