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,

I'm rather new to the 'mocking' concept. I've read Martin Fowler's paper on mock objects to get a bit up to speed.

This is my current understanding:

Unit tests should factor out as much as possible dependent services, systems and objects as long as they do not directly influence the core of the test. In order to do that we can create mock objects (in those scenarios where it is possible)

My question about Typemock is:

If we use Typemock does it mean we do NOT have to create, define or otherwise construct the mock objects explicitly. Typemock is creating those behind the scenes by using AOP concepts.

Is this assumption true?

Thanks in advance for feedback.

Regards,

Wilke
asked by wjansoone (600 points)

1 Answer

0 votes
Hi Wilke and welcome to the forum 8)

If you mean that you don't have to write your own stub class and use it in your test instead of the 'real' class than the the answer is yes.
You don't have to create it, Typemock Isolator will that for you.

However you still have to write some code that will instruct the Isolator how
your faked object should behave.

For example suppose you want to test ClassUnderTest.MethodToTest()

public class ClassUnderTest
{
    public int MethodToTest(ClassToIsolate classToIsolate)
    {
        int x = classToIsolate.MethodReturningInt();
        //do some logic here ...
        return x + 5;
    }
}

public class ClassToIsolate
{
    public int MethodReturningInt()
    {
        return 1;
    }
}


You want to isolate ClassToIsolate.MethodReturningInt and set it to return a faked value.

Here is the test:
[Test]
public void Test()
{
    //Arrange
    var fake = Isolate.Fake.Instance<ClassToIsolate>();
    Isolate.WhenCalled(() => fake.MethodReturningInt()).WillReturn(100);
    Isolate.Swap.NextInstance<ClassToIsolate>().With(fake);
    
    //Act
    ClassUnderTest classUnderTest = new ClassUnderTest();
    int actual = classUnderTest.MethodToTest(fake);

    //Assert
    int expcted = 105;
    Assert.AreEqual(expcted, actual);
}


As you can see you still have to write some code but it's much shorter, faster and less error prone than writing your own stub class.

You can see more detailed articles about the Isolator usage in our blog here
answered by ohad (35.5k points)
...