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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:35:18+00:00 2026-06-15T23:35:18+00:00

I need a way to fake DateTime.Parse with Typemock and have it return the

  • 0

I need a way to fake DateTime.Parse with Typemock and have it return the same date when called with any parameters.

I have a DB field that stores an encrypted string that is parsed as date when loaded. The class that holds the data has a Load() method where it copies DB data into its properties, decrypts what’s encrypted and does some basic validation, such as:

public class BusinessObject{
    public DateTime ImportantDate{get;set;}
...
    public void Load(DBObject dbSource){
        ...
        ImportantDate = DateTime.Parse(DecryptionService.Decrypt(dbSource.ImportantDate));
    }
}

Runtime all works well.

I’m trying to write a unit test using TypeMock to load some fake data into BusinessObject using its Load method. BusinessObject has way too many properties and can not be deserialized from XML, but DBObject can, so I’ve stored some XMLs that represent valid data.

It all works well until DecryptionService is called to decrypt the data – it doesn’t work because my dev machine doesn’t have the DB certificates used in the encryption process. I can’t get those on my machine just for testing, that would be a security breach.

I added this to my unit test:

Isolate.Fake.StaticMethods<DecryptionService>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => DecryptionService .Decrypt(null)).WillReturn("***");

Isolate.Fake.StaticMethods<DateTime>(Members.ReturnNulls);
Isolate.WhenCalled(() => DateTime.Parse("***"  /*DateStr*/)).WillReturn(DateTime.Now.AddYears(2));

The first part where DecryptionService is faked works, social security and other sensitive strings are “decrypting”, but no matter what parameters I give to DateTime I still get one exception or another (ArgumentNullException: String reference not set to an instance of an object if DateStr is null, FormatException when it’s “*“)

How (if) can I override DateTime.Parse with typemock so that it returns valid DateTime with any invalid paramters passed?

  • 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-06-15T23:35:18+00:00Added an answer on June 15, 2026 at 11:35 pm

    My name is Nofar and i’m from Typemock’s support team.

    DateTime.Parse is not supported in the WhenCalled API, so in order to fake it’s returned value you need to wrap it with a method from your class, for example:

    public class BusinessObject
    {
        public DateTime Load (string s)
        {
        return DateTime.Parse(s);  
        }
    }
    

    And you test will look like this:

        [TestMethod]
        public void TestMethodDateTime()
        {
            BusinessObject b = new BusinessObject();
            DateTime now= DateTime.Now.AddYears(2);
    
            Isolate.WhenCalled(()=>b.Load(null)).WillReturn(now);
    
            Assert.AreEqual(now, b.Load(null));
        }
    

    Supporting DateTime.Parse in the WhenCalled API is in our backlog.

    Please feel free to contact us via mail at support@typemock.com

    Nofar
    Typemock Support

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

Sidebar

Related Questions

I need a way to automatically format Date and Number objects based on locale
I have a sideshow using jQuery Cycle . The way it works is that
@Aniko points out that one way to view my problem is that I need
I have some code I need to write a test for that connects to
I need a way to change the specific value of a column of a
I need a way in my Chrome extension to know when the browser is
I need a way to take text from a EditText as soon as the
it need a way to call function whose name is stored in a string
I need a way to update tables on live machine without affecting users experience.
I need a way to know what are the children of an XmlNode allowed

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.