Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 586799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:08:40+00:00 2026-05-13T15:08:40+00:00

I have a class that relies on NSUserDefaults that I’m trying to unit-test and

  • 0

I have a class that relies on NSUserDefaults that I’m trying to unit-test and I’m providing the NSUserDefaults as a mock into my test class. When running the test, I get the error:

OCMockObject[NSUserDefaults]:
unexpected method invoked:
dictionaryForKey:@”response”

I’m trying to mock this instance method of the NSUserDefaults class:

- (NSDictionary *)dictionaryForKey:(NSString *)defaultName;

using the call format:

[[[mockClass stub] andReturn:someDictionary] dictionaryForKey:@"aKey"]

to tell the mock that it needs to expect the dictionaryForKey method. But somehow, this isn’t recorded or isn’t the right thing to tell the mock to expect since the error tells me that mock never knew to expect the ‘dictionaryForKey’ call.

The way I’m invoking the stub’s andReturn seems very similar to this question but in that one, the poster says they’re getting a return value.
My test case:

-(void)testSomeWork
{
    id userDefaultsMock = [OCMockObject mockForClass:[NSUserDefaults class]];       
    MyClass *myClass = [[MyClass alloc] initWith:userDefaultsMock];

    NSDictionary *dictionary = [NSDictionary dictionary];

    [[[userDefaultsMock stub] andReturn:dictionary] dictionaryForKey:@"response"];

    BOOL result = [myClass doSomeWork];

    STAssertTrue(result, @"not working right");

    [myClass release];
    [userDefaultsMock verify];
}

My class:

@implementation MyClass

@synthesize userDefaults;
- (id)initWith:(NSUserDefaults *aValue)
{
    if (self = [super init])
    {
        self.userDefaults = aValue;
    }
    return self;
}

- (BOOL)doSomeWork
{
    NSDictionary *response = [userDefaults dictionaryForKey:@"response"];

    if (response != nil)
    {
        // some stuff happens here
        return YES;
    }

    return NO;
}   
@end

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:08:41+00:00Added an answer on May 13, 2026 at 3:08 pm

    Not sure if you figured this out but it’s probably to do with using stub with verify. You should use verify with expect.

    i.e.

    [[[userDefaultsMock expect] andReturn:dictionary] dictionaryForKey:@"response"];
    ...
    [userDefaultsMock verify];
    

    In this instance you use verify to confirm that your method did in fact call the expected method (dictionaryForKey:). You use stub to allow your method to call a given method on a Mock Object but you don’t need to verify that it was called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unit test that relies on a random dice roll. I roll
I have a class that relies on HttpServerUtilityBase my plan was to get structure
I have class that represents users. Users are divided into two groups with different
I have a NetBeans project that relies on one specific Java class in another
I have a WinForms application that relies on a class library DLL. Inside the
I have an ASP.NET application that relies on the Random class to generate a
I have class method that returns a list of employees that I can iterate
I have a class that I want to use to store properties for another
I have a class that map objects to objects, but unlike dictionary it maps
I have a class that I wish to expose as a remote service using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.