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
Class:
class MyClass
{
  //Constructor
  public MyClass(string paramB)
  {
    if (paramB.Length > PropertyX)  thorow new ArgumentException();

    .... Code ....
    .... Code ....
  }

  public int PropertyX
  {
    get
    {
      int retVal = 0;
      ...<Code>...
      return retVal;
    }
  }
  
}


Test Class:
// Objective of this test is to make sure that 'ArgumentException' is thorwn in case if passed parameter's length gets bigger than PropertyX's value
[TestMethod()]
public void MyClassTest_TextLenMoreThanLimit_ThrowArgumentException()
{
   string paramB = "20 char long string";

   MyClass myObj = null;

   try
   {
     myObj = new MyClass(paramB);
   }
   Catch(Exception e)
   {
       Assert.AreEqual(typeof(ArgumentException), e.GetType());
       return;
    }
   Assert.Fail("Failed to return ArgumentException");
}


Now, inorder to test above scenario, I need to mock PropertyX's value to less than 20. I can do this by mocking PropertyX

The test will now look like -
[TestMethod(), VerifyMocks()]
public void MyClassTest_TextLenMoreThanLimit_ThrowArgumentException()
{
   string paramB = "20 chars. long string";

   MyClass myObj = null;

   using (RecordExpectations r = RecorderManager.StartRecording())
   {
     r.ExpectAndReturn(myObj.PropertyX, 19);
   }

   try
   {
     myObj = new MyClass(paramB);
   }
   Catch(Exception e)
   {
       Assert.AreEqual(typeof(ArgumentException), e.GetType());
       return;
    }
   Assert.Fail("Failed to return ArgumentException");
}


Its apperant that this test will fail, as my using block is trying to mock a property of a null object.
All my point here is, how will get the handle for 'this' - when I am testing a constructor.

-Vikas
asked by vikaschoithani (2.8k points)

2 Answers

0 votes
Hi Vikas,

hers how you can do it:
[TestMethod]
[VerifyMocks]
[ExpectedException(typeof(ArgumentException))]
public void TestMethod3()
{
    using (RecordExpectations rec = new RecordExpectations())
    {
        MyClass mock = new MyClass("dummy");
        rec.CallOriginal();
        int dummy = mock.PropertyX;
        rec.Return(3);
    }

    MyClass targe = new MyClass("way too long string");

}


:?: looking at your test I done think I get what exactly are you testing here.
i know that your example is probably simplified, however what is the logic behind "PropertyX"? It look strange to me that you expect the property to contain actual logic when the instance is not yet initialized.
Maybe it would make more sens for the property to be static?
answered by error (6.6k points)
0 votes
Hi Lior,

Thanks for the prompt reply and suggestion. My test is running now.

Actually you are right, the way I have written code in my first post, seems bit confusing.

In actual code, the class that I am testing - 'MyClass', is a Base class of many other classes and this class is an Abstract Class. Whatever logic or exceptions I am testing in 'MyClass' constructor are common for all derived classes. 'PropertyX' is not dependent on any instance from my classes, it calls a public-static method from some other class. It is a good practice to keep such properties STATIC. But in my case I can not make it static, because The 'PropertyX' is actually an ABSTRACT property, which is overridden is all base classes.

Hope this helps you understand the code better. Suggestions are welcomed.

Thank you,
Vikas
answered by vikaschoithani (2.8k points)
...