Using partialMockForObject to do singleton class mock, how to create a method like "removeAllExpectations” to remove all remembered ones?

Confused on OCMock for singleton, get two methods, but none of them is perfect in my opinion.

  1. Using partialMockForObject. I want to use partialMockForObject to do singleton class mock. See following code, it works well, but the defect is that singleton [NSNotificationCenter defaultCenter] will remember previous mock behavior, then when invoked in another place, it will crash, unexpected. So my question is that how can I create a method like "removeAllExpectations” to remove all remembered ones?

    id aMock = [OCMockObject partialMockForObject:[NSNotificationCenter defaultCenter]];

    [aMock expect] removeObserver:[OCMConstraint isKindOfClass:[WhereIsMyPhoneViewController class]]];

    [[aMock verify];

  2. Using category method. See http://twobitlabs.com/2011/02/mocking-singletons-with-ocmock/ mentions another method to do singleton mock, personally I prefer partialMockForObject, you don't need to mock all methods when unit testing.

Have sent my question to OCMock development group but no response got yet, need your smart guys opinion. Any idea or discussion will be appreciated, thanks in advance.

Answers


You can call:

[aMock stop];

to reset the mocked object's state.


If you are using the lastest source code of OCMock (after 2012-04-06), call

[aMock stopMocking];

to reset the mocked object's state.


Need Your Help

App stalls on addPersistentStoreWithType with iCloud

iphone ios objective-c core-data icloud

I have the following code in my persistentStoreCoordinator. Without the iCloud part, it works fine. With iCloud, it stops on the addPersistentStoreWithType method. No error, it just stops on it and

Difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);

math opengl graphics

What's the difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.