This project is read-only.

DateTime.Now not getting intercepted

Jul 24, 2012 at 6:18 PM


I new to Cthru and TypeMocks. I wrote a simple application and want to intercept DateTime.Now in the application. This is the code I have:

Aspect Class:

 class CThruAspectClass : Aspect 

{    public override bool ShouldIntercept(InterceptInfo info)   

{      return info.MethodName == "get_Now";    }

    public override void MethodBehavior(DuringCallbackEventArgs e)    {      base.MethodBehavior(e);
      e.MethodBehavior = MethodBehaviors.ReturnsCustomValue;
      e.ReturnValueOrException = new DateTime(1, 1, 1);    }


I have this in main:

 public static void Main(string[] args)   

{      var assembly = typeof(MockManager).Assembly;
      CThruAspectClass ctac = new CThruAspectClass();      


    string path = somepath;     

   Class1 cl1 = new Class1();     

   cl1.WriteTime(path);    }

The actual DateTime.Now method gets called in WriteTime which is in a different class. I read that mscorlib types cannot be intercepted in the main method but this should be ok, right?

I start up the application using TMockRunner.exe but I see that the current Date and Time get written not the overriden value. What am I doing wrong?

Any help will be appreciated!

Thank you!

Jul 26, 2012 at 2:24 PM


Could you try and run the following test:

			CThruEngine.AddAspect(new Stub(info => info.MethodName == "get_Now").Return(new DateTime(111)));

Works for me with the latest CThru

Jul 26, 2012 at 2:50 PM

Thanks for your reply.

I tried running your code but I'm having the same issue. The DateTime.Now value does not get overridden..

I put a writeline in the ShouldIntercept method in my AspectClass and I do not even see the DateTime.Now property being intercepted there. This is the output

CThru.BuiltInAspects.MissingMethodAspect .ctor
CThru.Aspect .ctor
CThru.CThruEngine AddAspect
CThru.CThruEngine GetUniqueKey
CThru.CThruEngine AddAspect
CThru.CThruEngine get_Verbose
CThru.CThruEngine+AspectHandler .ctor
CThru.CThruEngine add_DuringConstructorCall
CThru.CThruEngine add_DuringConstructorCall
CThru.CThruEngine add_DuringStaticConstructorCall
CThru.CThruEngine add_DuringStaticConstructorCall
CThru.CThruEngine add_duringMethodCall
CThru.CThruEngine add_duringMethodCall
CThru.CThruEngine add_missingMethodCall
CThru.CThruEngine add_missingMethodCall
ClassLibrary1.Class1 .ctor
ClassLibrary1.Class1 WriteTime
System.ComponentModel.Component Finalize
System.Diagnostics.Process Dispose
System.ComponentModel.Component Dispose

Does it matter that it is a property and not a method?


Jul 26, 2012 at 7:10 PM

No it doesn't matter.

What do you have in your output when you run my test? Here's what I get:

01.01.0001 0:00:00
You can see that it is actually the InterceptDateTime object that is intercepted.
What's your Isolator version?
Jul 26, 2012 at 7:19 PM

I managed to get it to run correctly, but I had to add the [TextFixture] attribute to the class and [Test] attribute to the program. Also, it only works correctly when i run it through visual studio, when I run it via the TMockRunner.exe, the property does not get intercepted. Is there any way to fix that? I am running Isolator V7.


Jul 26, 2012 at 7:38 PM

That better be asked at the Typemock Forums.

Jul 27, 2012 at 8:26 PM

I clarified this with them, thanks. I have another issue though. My application uses Spring .net and when I added CThru to it, it stopped working. In the typemock logs, I see it failing silently at this point

Exposing internals for module 122F1160Loaded: ->C:\Program Files (x86)\ITG\Custom\Active\bin\Spring.Core.dll

Is it not possible to have both enabled?

Jul 28, 2012 at 7:27 AM

Sorry, but that's better asked at the Typemock forums as well. This is related to some Isolator internals, which I don't understand at all.