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
Here’s the code I am trying to test.

Public class SomeOtherClass
{
        public static void SomeXMethod(int param1, bool param2)
        {
           .....
        }
}


Public class BaseClass : SomeOtherClass
{
        public int PropX
        {
          [Some complex logic to get data from database]
        }
}


public class DerivedClass : BaseClass
{

        protected DerivedClass(int Parameter1, int Parameter2)
            : base(Parameter2)
        {
            This.MyValue = Parameter1;
            SomeOtherClass.SomeXMethod(PropX, IsValid);
        }

        public bool IsValid
        {
            get
            {
               return [Gets some data from Database to check if MyValue’s value is valid];
            }
        }

   Public int MyValue{get;set;}


         Public DerivedClass StaticMethodThatCallsConstructor(int Parameter1, int Parameter2)
         {
            return new DerivedClass(Parameter1, Parameter2);
         }
}





Test:
        [TestMethod, VerifyMocks]
        public void DerivedClassConstructorTest_ThroughPublicAccessor()
        {
MockManager.MockAll<SomeOtherClass>().ExpectCall("SomeXMethod");
            using (RecordExpectations r = RecorderManager.StartRecording())
            {
                DerivedClass mock = DerivedClass. StaticMethodThatCallsConstructor (100, 101);
                r.CallOriginal();

                r.ExpectAndReturn(mock.PropX, 10).RepeatAlways();
                r.ExpectAndReturn(mock.IsValid, true).RepeatAlways();
            }

//Actual call to the constructor
            DerivedClass target = DerivedClass.StaticMethodThatCallsConstructor(100, 101);


//Some asserts to make sure assingments were properly done
Assert.IsNotNull(target);
        }


All I am trying to test here is, the assignments were done properly, and for that I am mocking some properties that hit database.

I've simplified this code, so it might not make sense, but this is the scenario where, I have mocked 'this' instance for derivedclass, but even then instance properties for derivedclass are not mocked.

Here, for some reason my ‘PropX’ and ‘IsValid’ do not get mocked and test fails during the call to these properties.

Thanks.
asked by vikaschoithani (2.8k points)

1 Answer

0 votes
The problem in the test is that you need to create a "real" object without invoking the two properties. You also need to fake the behavior of those properties.

Instead try to use the following:

[TestMethod, VerifyMocks]
public void DerivedClassConstructorTest_ThroughPublicAccessor()
{
    var mock = MockManager.Mock<DerivedClass>(Constructor.NotMocked);

    mock.AlwaysReturn("get_IsValid", true);
    mock.AlwaysReturn("get_PropX", true);

    //Actual call to the constructor
    DerivedClass target = DerivedClass.StaticMethodThatCallsConstructor(100, 101);

    //Some asserts to make sure assingments were properly done
    Assert.IsNotNull(target);
    Assert.IsTrue(target.IsValid);
}
answered by dhelper (11.9k points)
...