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,

While writing tests I realized that in some cases I am not able to verify all the parameters of a mocked constructor.
(I am using Typemock Isolator 5.3.1)

Let me show an example:

/* class that I am going to fake in my tests */
class ClassUnderTest
{
    internal ClassUnderTest()      {}
    internal ClassUnderTest(int i) {}
}

/* class faking ClassUnderTest that checks the parameters of the ctor */
class FakeClassUnderTest
{
    private Mock _mock = null;

    internal FakeOfToFake(params object[] args)
    {
        _mock = MockManager.Mock(typeof(ClassUnderTest));
        _mock.ExpectConstructor().Args(args);
    }
}


[Test]
public void TestConstructorTooParams()
{
    FakeClassUnderTest fake = new FakeClassUnderTest (1);
    ClassUnderTest classUnderTest = new ClassUnderTest();

    /* this test fail because 1 param is expected but constructor is called with 0 -> ok */
}

[Test]
public void TestConstructorTooFewParams()
{
    FakeClassUnderTest fake = new FakeClassUnderTest ();
    ClassUnderTest classUnderTest = new ClassUnderTest(1);

    /* this test succeeds while I expected to fail for a mismatch in the number of parameters */
}


How should I write my fake class FakeClassUnderTest to verify always the number of parameters?

Thanks in advance
-Simo
asked by simo (1.3k points)

5 Answers

0 votes
Hi Simo,

I will investigate this issue and inform you of possible solution.

On a side note verifying a specific constructor was called will probably result in a fragile test that would fail with every change to the class under test. In this case if you change the parameters passed to the constructor or the way it works your test would probably fail.

What are you testing in this case? perhaps there is a better way to test that scenario.
answered by dhelper (11.9k points)
0 votes
Simo,

I can confirm there's no easy way to verify constructor arguments in the AAA API. I too would like to see you are trying to test and try to suggest an alternative.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi,

Basically the test where we would verify the params was not a pure unit test, it was a kind of integration test.
Basically the class under test is a kind of manager that creates a number of private members using its constructor's parameters.
So, in our tests we would verify it provides the right parameters in the constructors of its private members.

Because not always we have getter-properties to access to these private members, we would "intercept" their creation (using ExpectConstructor) and directly verify the passed params.

Is there a way so that I can set an handler where I can access to the entire set of args?
That is, something like:

mock.ExpectCall("aMethod");
mock.MethodSettings("aMethod").MockMethodCalled += OnMethodCalled;

Regards
-Simo
answered by simo (1.3k points)
0 votes
Hi Simo,

You can use custom argument checker to do that.
inside your Check method you can implement the needed logic and compare the constructor arguments to the arguments you expected.
answered by ohad (35.4k points)
0 votes
Ok, I agree with you this seems to be the more feasible solution.

Thanks for your support
-Simo
answered by simo (1.3k points)
...