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 there,
Using TypeMock 4.1 with VS2008.

I'm confused about the usage of MockedInstance and the .Args of ExpectCall. Below is a simple test case that I can't figure out how to get working. I want to validate that Reactor.React is called with an Atom that has been instantiated with the correct arguments. Am I being braindead?:


using NUnit.Framework;
using TypeMock;

namespace TypeMockUsage
{
   [TestFixture]
   public class MockedInstanceConfusion
   {
      [SetUp]
      public void SetUp()
      {
         MockManager.Init();
      }


      [TearDown]
      public void TearDown()
      {
         MockManager.Verify();
      }


      [Test]
      public void TestMockedInstance()
      {
         Mock atom = MockManager.Mock(typeof (Atom));
         atom.ExpectConstructor().Args("Hydrogen");

         Mock reactor = MockManager.Mock(typeof (Reactor));
         reactor.ExpectCall("React").Args(atom.MockedInstance);

         PowerPlant.StartChainReaction();
      }
   }


   public class PowerPlant
   {
      public static void StartChainReaction()
      {
         Atom a = new Atom("Hydrogen");
         Reactor.React(a);
      }
   }


   public class Reactor
   {
      public static void React(Atom a)
      {
         a.Explode();
      }
   }


   public class Atom
   {
      private string _name;


      public Atom(string name)
      {
         _name = name;
      }


      public void Explode()
      {
         _name = "Helium";
      }
   }
}

________
Amc eagle specifications
asked by pmcevoy (4.7k points)

4 Answers

0 votes
Hi
The problem is that atom.MockedInstance is null when you use it in the test code.
This is because MockManager.Mock() will mock the NEXT call to
new Atom().
Check out this post in our blog for more details.

As a solution you can look at the Built-In Argument Checks in Typemock Isolator documentation.
I can think of two checks that may solve your problem
Check.IsTypeOf() or Check.IsMock()
[Test]
public void TestMockedInstance2()
{
   Mock atom = MockManager.Mock(typeof(Atom));
   atom.ExpectConstructor().Args("Hydrogen");

   Mock reactor = MockManager.Mock(typeof(Reactor));
   //choose one of the checks below
   //reactor.ExpectCall("React").Args(Check.IsTypeOf(typeof(Atom)));
   reactor.ExpectCall("React").Args(Check.IsMock(atom));

   PowerPlant.StartChainReaction();
}


Hope it helps :)
answered by ohad (35.4k points)
0 votes
Thanks Ohad!
________
Michigan Marijuana Dispensary
answered by pmcevoy (4.7k points)
0 votes
If you used natural mocks, and assuming that what you want to check is that Reactor is called correctly, you can implement "Equals" in the atom class and then write a test like this:

[Test,VerifyMocks]
public void StartChainReaction_Default_StartsReactorWithHydrogenAtom()
{
Atom a = new Atom("Hydrogen");
using (RecordExpectations r = new RecordExpectations())
{

Reactor.React(null);
r.CheckArguments(new ParameterCheckerEx(delegate(ParameterCheckerEventArgs data)
{
Assert.AreEqual(a,data.ArgumentValue);
return true;
}));

}

PowerPlant.StartChainReaction();
}
answered by royo (2k points)
0 votes
Thanks Roy,
Here is an even simpler (tm) way:

[Test,VerifyMocks]
public void StartChainReaction_Default_StartsReactorWithHydrogenAtom()
{
  using (RecordExpectations r = new RecordExpectations())
  {
      Atom a = new Atom("Hydrogen");
      Reactor.React(a);
      r.CheckArguments();
  }
  PowerPlant.StartChainReaction();
}
answered by eli (5.7k points)
...