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 was trying to figure out how to verify that a method is called within a constructor. So far, I have not been able to get it to work. Here is an example of the code and the unit test:

    public class ConfigData
    {
        public ConfigData(string fileName)
        {
            XmlDocument document = new XmlDocument();
            // document.Load(fileName); - commented out so file not needed
            ProcessFile(document);
        }

        private void ProcessFile(XmlDocument doc)
        {
            // Code to process XML file
        }
    }

...

    [TestClass]
    public class MyTestClass
    {
        [TestMethod]
        public void TestConfigData()
        {
            ConfigData configBase =
                Isolate.Fake.Instance<ConfigData>(Members.MustSpecifyReturnValues,
                ConstructorWillBe.Called, "Config.xml");

            Isolate.Verify.NonPublic.WasCalled(configBase, "ProcessFile");
        }
    }


My gut reaction is that this fails because a method cannot be faked without the object first being created. Since the method call I want to verify is in the constructor, the constructor is called prior to the default behavior for members being established. If that is indeed the case, then calling the constructor during creation of a fake object would mean that the original implementations would be used for any methods called from within the constructor. It would make the most sense if the default behavior for members was established prior to calling the constructor.

I am hoping that I am just missing something obvious here.

Thanks,
Brian
asked by bhunter (3.4k points)

4 Answers

0 votes
Hi Brian,

What you’ve described might be a bug, we've reproduced it and we're investigating to see what caused it. I'll update you on this issue.

Best regards,
Elisha Typemock Support
answered by Elisha (12k points)
0 votes
Thanks, Elisha.

Along these same lines, it would be nice if you could define the behavior on a non-static method prior to creating a fake object. As in my example above, it would provide the ability to fake methods called within constructors (assuming that the ConstructorWillBe.Called value is used when creating the fake object).

Brian
answered by bhunter (3.4k points)
0 votes
Hi Brian,

You're correct about the usefulness of this kind of feature.

One of the classic cases is defining faked behavior to factory method inside the class.

For example:
public class ClassWithFactoryMethod
{
    private IService service;

    public ClassWithFactoryMethod()
    {
        service = CreateService();
    }

    IService CreateService()
    {
        return new Serivce();
    }
}


Setting the behavior of CreateService in advance could assist injecting a fake service.

Even though for this specific case Isolator can provide a good solution using swap:
Isolate.Swap.NextInstance<IService>().With(fakedService)


Thanks for the suggestion. I'll update you on this issue.

Best Regards,
Elisha
Typemock Support Team
answered by Elisha (12k points)
0 votes
Hi Brian,

We verified the issue you initially described is a bug (expectation on a call from the constructor).

Thank you very much, we'll fix it in an upcoming version.

Best regards,
Elisha,
Typemock Support Team
answered by Elisha (12k points)
...