Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock internal calls of a another class methods #156

Closed
PoojaNaik01 opened this issue Mar 17, 2020 · 4 comments
Closed

Mock internal calls of a another class methods #156

PoojaNaik01 opened this issue Mar 17, 2020 · 4 comments

Comments

@PoojaNaik01
Copy link

PoojaNaik01 commented Mar 17, 2020

I am facing issues mocking calls of another class from a class which i am unit testing.
When the method2 is called from Class1, the call doesn't reach to mockCalls to return desired object.

Code to unit test Class1:

- (void)test_success {
   self.restService = mock([Class2 class]);
   [self mockCalls:self.restService withResult:someObject];
   Class1 *sut = [[Class1 alloc] initWith:self.restService];
    XCTestExpectation *expectation = [self expectationWithDescription:@"expectation"];
    [sut someMethod:@"abc" parameter2:@"123456" parameter3:@"abcd" success:^(someObject) {
        [expectation fulfill];
        XCTAssertNotNil(someObject, @"object was null");
    } failure:^(NSError *error) {
        XCTFail(@"NSError not expected");
    }];
    
    [self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
        if (error != nil) {
            XCTFail(@"Expectation Failed with error: %@", error);
        }
    }];
}

- (void)mockCalls:(Class2 *)mock withResult:(SomeClass *)someObject {
    [givenVoid([mock method2:anything()
                                            parameter2:anything()
                                         parameter3:anything()
                                                  success:anything()
                                                  failure:anything()]) willDo:^id (NSInvocation *invocation) {
        NSArray *args = [invocation mkt_arguments];
        void(^success)(someObject) = args[3];
        success(someObject);
        return nil;
    }];
}

Can you please help @jonreid ?

@jonreid
Copy link
Owner

jonreid commented Mar 21, 2020

@PoojaNaik01, set a breakpoint inside the willDo block. Is it reached?

@PoojaNaik01
Copy link
Author

PoojaNaik01 commented Mar 22, 2020 via email

@jonreid
Copy link
Owner

jonreid commented Mar 22, 2020

This is a lot to ask, but could you share a minimal project with me that demonstrates the problem? Then I can explore on my machine with the debugger.

@jonreid
Copy link
Owner

jonreid commented Jun 6, 2021

Closing for lack of activity. Please reopen if you are still interested.

@jonreid jonreid closed this as completed Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants