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 7616275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:51:01+00:00 2026-05-31T02:51:01+00:00

There’s a rule that says Only mock objects that you own. I think I

  • 0

There’s a rule that says

Only mock objects that you own.

I think I understand the reason for this – mocking classes supplied by the frameworks might result in strange behaviour.

What’s the alternative?

What about when you need a dummy date using NSDate?

In the past I’ve swizzled the date method from NSDate to my own class – NSDateMock – but something tells me that’s really wrong!

One Solution – A Wrapper?

Create a wrapper round NSDate, but then you’d have to implement all it’s methods.

Or would you just implement the ones you were using? It seems a messy way of doing it.

My Question

What’s a good way of mocking classes you don’t own such as NSDate?

Update 1

I found this article on mocking which seems to imply that writing a thin wrapper is the way to go. I’m not quite sure why but I feel like this is a hack. Then again, it could make the code more expressive.

But that raises the question, in the case of NSDate do you inject the wrapper class into every single class that needs to know the date?! Surely not…

Update 2

There have been some good answers on this question, but I’m still holding out for other answers – there must be a definitive way of doing this, surely? I still don’t see how categories are going to give me a dummy object I can control either.

  • 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-31T02:51:02+00:00Added an answer on May 31, 2026 at 2:51 am

    In general this is a “how to mock a static/class level method” question, which googling shows a lot of different ideas, so it mainly comes down to taste.

    I consider partial mocks to be a code smell as it shows that your class under test (CUT) isn’t testable -> time to refactor.

    I’ve tackled this in 2 ways in the past:

    1.) Pass a DateProvider (which is an interface) into the constructor of the CUT

    interface DateProvider 
        date timenow()
    end
    

    At test time this is your MockDateProvider which you can change the state from your test class. I might use a public static field that I can change in my tests

    class MockDateProvider :: DateProvider
       public static field fakeDate   
       date timenow () 
          return fakedate
       end
    end   
    

    In the real system it just has the date creation method you used.

    class RealDateProvider :: DateProvider
       date timenow()
          return LibraryDateMaker.newDate()
       end
    end
    

    I might make 2 constructors, one that takes this interface and another which uses the RealDateProvider without any production classes needing to pass in the object.

    This is the preferable OO way of doing things. I think!

    2.) Make your own static date provider that you can override the behaviour of.

    Rather than LibraryDateMaker.newDate() you make a ConfigurableDateMaker.newDate() static. This uses the same objects as 1, but has a setter to allow you to change the behaviour to the mock provider as required. Defaulting to real.

    The advantage of this is you don’t have to pass anything into your constructors and you get to keep using a static method for a very common activity.

    In brief, the CUT calls ConfigurableDateMaker.newDate(). Which by default returns a real date, but in your test class you can set the behaviour to use the mock before calling the CUT.

    class ConfigurableDateMaker
      public static DateProvider provider
       static date timenow()
          return provider.newDate()
       end
       // add the 2 provider classes as inner classes in here
    end
    

    Hopefully that makes some sense.

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

Sidebar

Related Questions

There's a common error that gets thrown by the Visual C Runtime: This application
There are a lot of SO questions that seem to address variations of this
There is a website that support shops information like food,drink,golf,spa. This websites provide information
There is this SQL Statement SELECT t1.Name ,Count(t2.SubID) Totals -- I don't know how
There are numerous places on the Internet, suggesting that it is easily achieved by
For some reason, after submitting a string like this Jack’s Spindle from a text
There is a website called Gild.com that has different coding puzzles/challenges for users to
There is this strange situation I'm fighting on. I have 3 pages, les call
There is no doubt that MonoTouch is one of the great cross-compiler(s). Similarly, SenchaTouch
There is probably an easy answer for this, but when I added DateTime.Now.ToString() to

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.