locked
DateTime.Now does NOT update when using [HostType("Moles")]? RRS feed

  • Question

  • I have run into a strange problem and I'm hoping someone can tell me if I'm doing something wrong or this is a Moles bug.

     

    I have a test that needs to run a while loop and check the elapsed time by repeatedly calling DateTime.Now.

     

    DateTime startTime = DateTime.Now;
    bool isWait = true;
    
    while (isWait)
    {
       DoStuff();
    
       TimeSpan elapsed = DateTime.Now - startTime;
       isWait = elapsed < TimeSpan.FromSeconds(1);
    }
    

    Yes, I know this loop results in high CPU usage but due to a complicated issue, I cannot block the thread that is running this loop, hence this polling wait.

     

    What I have found out is when I mark my test with [HostType("Moles")], the first call to DateTime.Now works, but subsequent calls all return the exact same time as the first.  This makes my loop run forever and can never break out of it.

     

    When I remove the [HostType("Moles")], however, DateTime.Now works as expected and the loop correctly exits.

     

    Does anyone know why?  I'm using Moles 0.94.

    Wednesday, February 16, 2011 2:37 AM

All replies

  • I have the same issue. I'm trying to add unit tests "as an afterthought" to a brown field project but I'm unable to test code that relies the progresion of DateTime.Now.
    Thursday, February 24, 2011 1:31 PM
  • How do you mole DateTime.Now? Make sure the delegate increments on each call.
    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Friday, February 25, 2011 7:04 PM
  • Well that's exactly my point Peli, I DIDN'T mole DateTime.Now, and it won't update if I mark the test to run under the Moles host type!
    Saturday, February 26, 2011 1:06 AM
  • Do you use any other Moles related code in the test case?


    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Saturday, February 26, 2011 1:10 AM
  • Yes, I do.  But the moled code is not related to DateTime.Now.
    Saturday, February 26, 2011 1:13 AM
  • Do you use any kind of fallback behaviors?
    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Saturday, February 26, 2011 1:14 AM
  • I don't think so.  At least, not that I'm aware of.  I just used moles like I do regularly.

     

    What I'm doing is trying to unit test a WPF custom control and since the control uses a DispatcherTimer I need to use DateTime.Now to wait a certain time for the control's DispatcherTimer to trigger a "time's up" callback.  

     

    Since the control calls GetTemplateChild() to get its template parts from the control template, I use Moles to override the GetTemplateChild() call to return a fake template part so my custom control can continue to function correctly.  That's the extent where I use Moles, and nowhere else.

     

    I'm not sure if using WPF with Moles causes some problem with DateTime.Now, but since Martin Liversage has reported the same problem I'm guessing it's not because of WPF (unless he's trying to test WPF related stuff too).

     

    Thanks for your help!

    Saturday, February 26, 2011 1:21 AM