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
I have two objects, object 1 is in C# and is what I am trying to test. However, it relies on object 2, which is C++ COM. Basically, the problem is a value that is coming from object 2 is wrong because of bad data, so I have code in the C# getter that calls through to the COM getter, and if it determines the data is wrong, it applies a few rules and updates the value in the COM object, and then returns. The code below is from the C# object, and this.Private is the COM object. The problem is
this.Private.Number = num;
does nothing.

public string Number
{
   get
   {
      if (this.Private.Number.IndexOf("/") == -1)
      {
         string num = this.Private.Number;
         // Apply some rules based on other stuff...
         this.Private.Number = num;
      }
      return this.Private.Number;
   }
}


So, my test looks like this:
[TestMethod]
public void TestNumber()
{
   MockObject<TestObject> mock = MockManager.MockObject<TestObject>(); // The C# class with the method under test
   MockObject<Private> mockPrivate = MockManager.MockObject<DOCUMENTLib>(); // The COM object
   mockPrivate.ExpectGetAlways("Number", "12345");
   mock.ExpectGetAlways("Private", mockPrivate.MockedInstance);
   // Number should add the 01 to fix data issues
   Assert.AreEqual("01/12345", mock.MockedInstance.Number);
}


Please don't ask why I am not using the AAA framework, that is not the point of this.
asked by nbrett (1.7k points)

2 Answers

0 votes
If I understand the test correctly you're trying to verify that Private.Number is set is called with the value "01/12345".

As far as I understand from the code Private is a C# property:
public DOCUMENTLib Private
{
    get{ return someComObject; }
}


If that is the case you need to update your test:
MockObject<TestObject> mock = MockManager.MockObject<TestObject>(); // The C# class with the method under test
   MockObject<DOCUMENTLib> mockPrivate = MockManager.MockObject<DOCUMENTLib>(); // The COM object
   mockPrivate.ExpectGetAlways("Number", "12345");

   // Check if the set was called with the expected arguments
   mockPrivate.ExpectSet("Private").Args("01/12345");

   mock.ExpectGetAlways("Private", mockPrivate.MockedInstance); 

   MockManager.Verify(); // You can use [VerifyMocks] attribute instead of this line
answered by dhelper (11.9k points)
0 votes
That worked, thanks!
answered by nbrett (1.7k points)
...