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
Typemock can already fake a method call/mock external objects, simulate the return, and verify/assert the returned object.

NUnit can do exactly the same thing - except NUnit can't do mocking. So it seems Typemock can do all that NUnit does and more.

With this being the case, why do I need to use both a unit testing tool and Typemock? If I do need both, what would a class using both look like? Where would both tools fit in?


Thanks
asked by GSS (2.8k points)

3 Answers

0 votes
Hi

The Isolator and NUnit should be used side by side.
NUnit is a test runner - it only runs tests that are decorated with NUnit attributes.
The Isolator is a framework that handles faking objects, it can not run unit tests.

Hope it helps, please let me know if you have more questions.
answered by ohad (35.4k points)
0 votes
Hi,

Does that mean my test code would need NUnit attributes?

What I am confused on is NUnit and Typemock both provide similar functionality. In my actual test code, do I need to use both APIs at any time?

Imagine I am writing a test class for a method which gets data from a database and processes it. In typical like this (or advanced cases), do I need the API of NUnit as well as Typemock?

Thanks
answered by GSS (2.8k points)
0 votes
Hi

Yes, you'll need the nunit attributes in order to run the tests.
You should create a separate assembly for your unit tests and add references for nunit and Isolator assemblies.

The Nunit runner will run tests in public classes that are marked with TestFixture attribute and public test methods that are marked with Test attribute.

Nunit API provides you the Assert class which includes many methods that let you assert at the end of the test that you got the expected results.

Consider the (simplified) example below.
This is your production code:
public class ClassUnderTest
{
    public int MethodUnderTest(ClassToFake classToFake)
    {
        if (classToFake.MethodToFake() >= 100)
        {
            // do some logic ...
            return 101;
        }

        // do some logic ...
        return 0;
    }
}

public class ClassToFake
{
    public int MethodToFake()
    {
        return 1;
    }
}


This is your unit tests code in separate tests assembly:
[TestFixture] // nunit attribute
public class TestClass
{
    [Test] // nunit attribute
    public void TestMethod()
    {
        // Isolator API.
        ClassToFake fake = Isolate.Fake.Instance<ClassToFake>();
        // Isolator API.
        Isolate.WhenCalled(() => fake.MethodToFake()).WillReturn(100);

        // Call the code under test.
        ClassUnderTest classUnderTest = new ClassUnderTest();
        int actual = classUnderTest.MethodUnderTest(fake);

        // NUnit API. Assert the expected results
        Assert.AreEqual(101, actual);
    }        
}


Hope it clears things up.
answered by ohad (35.4k points)
...