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
0 votes
Hi,

is it possible to unmock a Type somehow?
asked by tolisss (28.8k points)

6 Answers

0 votes
Hi,
You can use mock.Clear() to clear all the expectations, this will make the Type unmocked
answered by scott (32k points)
0 votes
Hi,
let me explain more the situation

I have a class that inside a method mockAll(someType) and i want to use mockAll(someType) b4 executing the method of the above class and then execute the method but when i do i get that i cannot MockAll the same type twice. Any way to overcome this?

MockManager.Init();
         Mock mockAll = MockManager.MockAll(typeof(Class1));
            mockAll.Clear();

         MockManager.MockAll(typeof(Class1));
answered by tolisss (28.8k points)
0 votes
Hi,
Sorry, I don't really understand what the scenario is.
Can you please explain in more detail?

Why not do a Clear and setup your expectations again?
answered by scott (32k points)
0 votes
k i will explain more...
i have build a Class to test GridControls. That Class make extensive use of
MockManager.MockAll(typeof(GridControl));


and i call that class in a formFixture test method
[TestFixture]
   public class FormFixture
   {
      [Test]
      public void testGridControl()
      {
         GridTester tester=new GridTester("gridControlName");
            tester.AssertGridView();
      }
   }





suppose that i want to make a setup method also like

[SetUp]
      public void SetUp()
      {
         Mock mockGridControl = MockManager.MockAll(typeof(GridControl));
            mockGridControl.ExpectSet("DataSource").Args(Check.IsAny());
            mockGridControl.Verify();
            mockGridControl.Clear();
      }

but when i try to execute the whole fixture
[SetUp]
      public void SetUp()
      {
         Mock mockGridControl = MockManager.MockAll(typeof(GridControl));
            mockGridControl.ExpectSetAlways("DataSource").Args(Check.IsAny());
            mockGridControl.Verify();
            mockGridControl.Clear();
      }

      [Test]
      public void testGridControl()
      {
         GridTester tester=new GridTester("gridControlName");
            tester.AssertGridView();
      }


it will fail with the message that i cannot MockAll the same GridView twice (cuase AssertGridView uses MockManager.MockAll(typeof(GridControl));)

of course i could overcome the problem creating an overload of AssertGridView method with a mock object as parameter, but i just wondering if there is another way

hope i make myself clearer this time
answered by tolisss (28.8k points)
0 votes
Hi,
I understand your problem better now, currently you have to use the same mock in all your tests, So as you suggested, either send the mock as a parameter to AssertGridView(), or have the mock static in GridTester, then from the setup do a GridTester.GridControlMock.
answered by scott (32k points)
0 votes
Hi,
In Version 2.3 you can now access the Mock from the framework

Example:
private Mock SetUpFactoryMock()
{
   // Initialize now if not initialized already
   if (!MockManager.IsInitialised) MockManager.Init ();
   
   Mock theMock; 
   // Get mock from framework or create a new mock
   if (MockManager.IsTypeMockedAll(typeof(TestedClass)))
   {
      theMock = MockManager.GetMockAll(typeof(TestedClass)); 
   } 
   else
   {
      theMock = MockManager.MockAll(typeof(TestedClass)); 
   } 
   // add expectations ... 
   
   return theMock;
}   
answered by scott (32k points)
...