chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

I'm a beginner and just starting to use TypeMock.
I have a scenario where we need to run regression/integration tests on our application and need to check how it behaves at various times. We cannot change the system time for this purpose and I was hoping to achieve this by mocking DateTime.Now. I added the line to mock DateTime.Now in the main method and while the application builds fine, it throws exceptions when I run it. Can TypeMock be used only in the unit testing context? I even tried to start up the application through the tmockrunner.exe but it still threw errors. Any help on this will be appreciated!

Thanks!
asked by SV1832 (3.4k points)

15 Answers

0 votes
Hi,

It is best practice to use Isolator in unit testing context.

If I understand your problem (please correct me if I'm wrong), you are trying to automate a test. what you have to do is to create a c# test project.
After you have a test project you can create test methods.

Inside your test methods is where you should use Isolator.

If that is not what you meant, Please let me know.
answered by alex (18k points)
0 votes
Hi Alex,

I do not need to unit test. Essentially, I would like to create a separate assembly for regression/integration testing with every instance of DateTime.Now mocked to a certain date and time. I would like to know if this is even possible i.e mocking in the main project and not in a test project.
Thanks for your time!
answered by SV1832 (3.4k points)
0 votes
Hi,

Thanks for the explanation, What you trying to do is possible with CThru.

Basically, what you have to do is:

1) Add CThru to your references.
2) Write a custom aspect class that inherits Aspect class.
3) Add the custom aspect to CThruEngine.
4) Override Should intercept in the custom aspect class
This method should return bool on every class method that you want to intercept.
5) Override MethodBehavior method in the custom aspect class
In this method you should specify the desired behavior of the intercepted method.
6) Load Typemock assembly in your main (to enable CThru).

* Intersaption of types from mscorelib.dll is not possible in the main assembly.

See the example below:

 class MyAspect : Aspect
    {
        public override bool ShouldIntercept(InterceptInfo info)
        {
            // check if the getter of "Now" property should be intercepted
            return info.MethodName == "get_Now"; 
        }

        public override void MethodBehavior(DuringCallbackEventArgs e)
        {
            base.MethodBehavior(e);

            // allow the method to return custom value
            e.MethodBehavior = MethodBehaviors.ReturnsCustomValue;

            // set the value you want to return
            e.ReturnValueOrException = new DateTime(1, 1, 1);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
             //force loading Typemock assembly.  
            var assembly = typeof(MockManager).Assembly;

            CThruEngine.AddAspect(new MyAspect());
            CThruEngine.StartListening();

            //Class1 is not in the main assembly so its call to dateTime.Now will be intercepted
            Console.WriteLine(new Class1().DoDo());
        }
    }


namespace ClassLibrary1
{
    public class Class1
    {
        public DateTime DoDo()
        {
            return DateTime.Now;
        }
    }
}


Please let me know if it helps.
answered by alex (18k points)
0 votes
Hi Alex,

I did something similar in my code:

This is my 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);
}

And this is my main method:
public static void Main(string[] args)
{
var assembly = typeof(MockManager).Assembly;

CThruEngine.AddAspect(new CThruAspectClass());
CThruEngine.StartListening();

string path = somepath;
Class1 cl1 = new Class1();
cl1.WriteTime(path);
}

The WriteTime method in class Class1 calls DateTime.Now. It is not being called in the main method so it should be getting intercepted right? I started up the project using TMockRunner.exe. However, I still see the current DateTime.Now being written and not the overloaded value. I'm probably doing something wrong but I can't seem to figure out what..

Thanks!
answered by SV1832 (3.4k points)
0 votes
Hi,

The code that calls methods from mscorelib.dll should be outside the assembly where the main method is.

In this case, try moving class1 to a different assembly.

Looking forward to your feedback.
answered by alex (18k points)
0 votes
Hi Alex,

I moved it to a separate dll but that didn't work for me either :( I still see the current date time being written..
Does it matter that DateTime.Now is a property and not a method? Will the methods in the aspect class be overridden in the same way?
Thanks for being so helpful!
answered by SV1832 (3.4k points)
0 votes
Hi Alex,

I moved it to a separate dll but that didn't work for me either :( I still see the current date time being written..
Does it matter that DateTime.Now is a property and not a method? Will the methods in the aspect class be overridden in the same way?
I put a writeline in the ShouldIntercept method and I don't see the DateTime.Now property being intercepted.
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

Thanks for being so helpful!
answered by SV1832 (3.4k points)
0 votes
Hi,

Please set Isolator to run with logs and send them to us.

You can enable logs in:
start menu -> Typemock-> Isolator -> configuration ->Visual studio tab
answered by alex (18k points)
0 votes
Hi Alex,

I managed to get it to run in visual studio. I had to add the [TestFixture] attribute to the class and [Test] attribute to the program (I am using nUnit). It now works when I run through VS but still not when I run via TMockRunner. Is there something else I need to set to get it to run through TMockRunner?
Also, I'm not able to generate logs for the TMockRunner run, I suppose the parameter only enables logging for the visual studio run? Is there an email id I can send the logs to, I can't seem to attach them with the message.

Thanks!
answered by SV1832 (3.4k points)
0 votes
Hi,

You can email the logs to support at Typemock.com.
answered by Elisha (12k points)
...