Giter Club home page Giter Club logo

Comments (4)

dmaclach avatar dmaclach commented on May 24, 2024 1

It's not a bug. You have a basic misunderstanding of what the code is doing. OCMClassMock does not return a class level mock, it's returning an "mock" instance of the class. Please follow through with posting to stack overflow so that others can easily find the answer if they have similar questions.

from ocmock.

liuxuan30 avatar liuxuan30 commented on May 24, 2024

UPDATE:

when I try a simplified version to test whether the mock is working:

- (void)testMock {
    id policyMock = OCMClassMock([PrivacyPolicy class]);
    OCMExpect([policyMock hasPermissionFor:OCMOCK_ANY scene:OCMOCK_ANY]).andReturn(YES);
    
    PrivacyConfig *config = [PrivacyConfig currentConfig];
    PrivacyPolicy *policy = [config getPolicyWithPermission:TestPermissionKey];
    
    BOOL val = [policy hasPermissionFor:KSPKTestPermissionKey scene:@"test"];
    XCTAssertTrue(val);
    OCMVerify([policyMock hasPermissionFor:OCMOCK_ANY scene:OCMOCK_ANY]);  // fail
}

When I tried to explicitly call hasPermissionFor:scene: it seems no matter with or without .andForwardToRealObject().andReturn(YES) the XCTAssertTrue and OCMVerify never pass, indicating the mock is not working at all.

I'm not sure if this is a limitation, or bug? because it seems PrivacyPolicy is a very simple class and nothing special.

from ocmock.

erikdoe avatar erikdoe commented on May 24, 2024

This looks like a problem with using OCMock. What is the object returned by getPolicyWithPermission:? Assuming that this is not the mock object, how would the mock notice that a method is called on that object?

Please do not ask follow-up questions here. Questions relating to the use of OCMock should be asked on StackOverflow. Thank you.

from ocmock.

liuxuan30 avatar liuxuan30 commented on May 24, 2024

@erikdoe I do hesitated where to ask before opening this thread, but eventually I thought I have read the doc and thinking it's worthy of confirming whether it's a bug or not.

Because in the doc

id mock = OCMClassMock([SomeClass class]);
OCMStub([mock someMethod]).andReturn(myValue);

/* run code under test */

OCMVerify([mock someMethod]);

it seems I can hook a instance method on a Class level mock?

getPolicyWithPermission returns a PrivacyPolicy instance, which I assume

id policyMock = OCMClassMock([PrivacyPolicy class]);
OCMExpect([policyMock hasPermissionFor:OCMOCK_ANY scene:OCMOCK_ANY]).andReturn(YES);
   

should work for this returned object? Could you confirm this is not a bug?

e.g. how could I mock a instance method for all instance for a specific class? Thanks.

from ocmock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.