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:

Is there anything like getting the instance of the real type from the mocked type as following?


Mock m = MockManager.Mock(typeof(Cart));

Cart c = (Cart)m.MockInstance;

I remember we can do this kind of stuff in NMock. thanks in advance.
asked by rxraza (1.8k points)

9 Answers

0 votes
Is there anything like getting the instance of the real type from the mocked type as following?

Hi,
TypeMock.NET has more functionality then nMock.
nMock creates an instanciation of an interface, you can do this in TypeMock using MockManager.MockObject:
MockObject mockCommand = MockManager.MockObject(typeof(ICart));
// create an object that implements ICart
ICart cart = (ICart) mockCommand.Object; 

but TypeMock is much more powerfull, you can mock an object that will be created in the future and NOT passes as a parameter:
Mock m = MockManager.Mock(typeof(Cart)); 
// somewhere deep inside the code there is the following line
Cart c = new Cart();
// The above instance of Cart will be mocked


So if you want to mock a concrete type and send it as a parameter you can do the following:
Mock m = MockManager.Mock(typeof(Cart)); 
Cart c = new Cart();
// The above instance of Cart will be mocked
answered by scott (32k points)
0 votes
Thanks for the reply Scott.

MockObject mockCommand = MockManager.MockObject(typeof(ICart));
// create an object that implements ICart
ICart cart = (ICart) mockCommand.Object;

Using MockManager.MockObject() to mock an object and then getting the property Object out of the mocked object makes perfect sense to me, that way I can return the instance to the caller

But I could not pick up your second statement

you can mock an object that will be created in the future and NOT passes as a parameter'

Can you please explain more about it. thanks

My goal is to mock an object an pass that object to the caller
answered by rxraza (1.8k points)
0 votes
While calling MockManager.MockObject()

I got the following runtime error

MockInserter not running, please run mocking_on.bat before running the tests

I am running unit test from the GUI and I do not know how do I run the batch file before firing the unit test GUI
answered by rxraza (1.8k points)
0 votes
Hi,
To run the MockInserter see the first topic in this forum: MockInserter not running, please run 'mocking_on.bat'

To understand instance mocking you can see the instance mocking section of the User Guide and see the instance mocking example that can be installed with TypeMock.

In short you can mock a type that will be created inside your code, so for example if you have:
public class TestedClass
{
   // This is bad design but is used just to show the abilities of TypeMock
   public int something;
   // public method
   public void PublicMethod()
   {
      PrivateMethod();
   }
   // private method will instantiate our class
   private void PrivateMethod()
   {
      YourClass c = new YourClass();
      something = c.DoSomething();
   }
}

Suppose that we want to test the PublicMethod, but mock DoSomething, the following code will tell typemock to mock a future instance of YourClass (this is not passed as a parameter)
[Test]
public void ThirdInstance ()
{
      // Initialize Type Mocks (This can be done in the [Setup])
      MockManager.Init ();
       // Mock and add expectations for next instance of YourClass
      Mock mock = MockManager.Mock(typeof(YourClass)); 
      // set up our expectations 
      mock.ExpectAndReturn("DoSomething",5);

      // lets run our test
      TestedClass t = new TestedClass();
      t.PublicMethod();     

      // something should be set to 5
      Assert.IsEqual(5,t.something);        
      // Verify that all calls were made
      MockManager.Verify();
}
answered by scott (32k points)
0 votes
I did the following:

MockObject mockObject = MockManager.MockObject(typeof(Cart));
where Cart is a concrete class

AND I GOT THE FOLLOWING ERROR

'Cannot create dynamic mocks from concrete classes, use Type mocks instead'

Is there anyother way of creating mocks for concrete classes other than use type Mocks?
answered by rxraza (1.8k points)
0 votes
Just use MockManager.Mock() :)
There is a difference between MockManager.Mock and MockObject.
:arrow: The MockObject creates a new object that implements an interface (This is your regular mock, like nMock)
:arrow: The Mock will Mock the next (or all) instances of a concrete class.

So to mock a concrete class do the following:
// set up the next instance of Cart to be mocked
Mock mock = MockManager.Mock(typeof(Cart));
// set up expectations...
mock.ExpectAndReturn("Method",5);
// Create a new Cart - this will be mocked
Cart thisIsAMockedCart = new Cart(); // no need to do mock.Object
                  // TypeMock is clever enough to insert your expectations
                  // into this Cart object, it listens to 'new' statments and 
                  // returns a mock instance (Wow!)
// now if we call Method it will return 5 (our mock)


:idea: Perhaps MockAll will be clearer. MockAll mocks all objects of the mocked type
// set up all Cart objects to be mocked
Mock mock = MockManager.MockAll(typeof(Cart));
// Create a new Cart - this will be mocked
Cart thisIsAMockedCart = new Cart(); // no need to do mock.Object

Perhaps the names are confusing
-MockObject should be called MockInterface as it mocks interfaces and abstract classes and creates an object
-Mock should be called MockTheNextInstanceOf.

We have spent a lot of resources on documenting TypeMock, you can find lots of comprehensive information there, Plus there are some articles on this site.
answered by scott (32k points)
0 votes
sure I will read that documentation

What I want to get done is a way to create Mock Classes for objects, so that clients can get the required data when they make function calls for instance in order to get the mocked cart a client would call

TypeMockCart.Instance

This would isolate the unit test code from the code responsible for doing mokcing.

I know that MockManager won't work in the class below. Please take a look at that and suggest me if that is doable or not. Please bear with me if I am taking a lot of your time. :D

Let me put it in this way, I need a custom Mock Class for instance TypeMockCart which would return the mocked Cart object as one of the property. I hope I make enough sense.

Thanks for your time.

public class TypeMockCart
{
private static readonly MockObject mockObject;

static TypeMockCart()
{
MockManager.Init();
mockObject = MockManager.MockObject(typeof(Cart));
Configure();
}

public static Cart Instance
{
get
{
return (Cart)mockObject.Object;
}
}

public TypeMockCart()
{
//
// TODO: Add constructor logic here
//
}


private static void Configure()
{
// Setup Behavior here

Cart c = CartObject.Create();
mockObject.ExpectGet("Items",c.Items);
}
}
answered by rxraza (1.8k points)
0 votes
Hi,
Here is how it can be done:
public class TypeMockCart
{
  private static Mock mock;
  private static Cart cart;
            
  static TypeMockCart()
  {
     if (!MockManager.IsInitialized) 
     {
         MockManager.Init();
     }
     mock = MockManager.Mock(typeof(Cart));
     Configure();
  }
            
  public static Cart Instance
  {
     get { return cart;   }
   }
      
   private static void Configure()
   {
      // Setup Behavior here   
      cart = new Cart();
      mockObject.ExpectGet("Items",c.Items);
    }
}

Notice:
1. The check for MockManager.IsInitialized
2. Creating a new Cart in Configure
answered by scott (32k points)
0 votes
Hi,
Version 2.3 supports creating a mock object from concrete classes using MockObject.

For example we can create a mocked concrete class of TestedClass without mocking the constructor and passing "Init" to the constructor.

[Test]
public void DynamicConcrete() 
{ 
   // Initialize
   MockManager.Init()
   // Mock our class 
   MockObject mock = MockManager.MockObject(typeof(TestedClass),Constructor.NotMocked,"Init");
   // Lets mock a return of 4 for Count 
   mock.ExpectGet("Count",4);
   // Get our concrete mocked Object
   TestedClass test = (TestedClass)mock.Object;
   // Let just test it
   Assert.AreEqual(3,test.Count);
   MockManager.Verify();
}

See documentation for more information
answered by scott (32k points)
...