This is a migrated thread and some comments may be shown as answers.

Create Mocks in a Testing Base Class

1 Answer 70 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Joshua
Top achievements
Rank 1
Joshua asked on 31 Jul 2013, 02:52 PM
Hi, I am running into a small issue with justmock. I am using nunit as my testing framework which allows base classes with multiple TestFixtureSetup methods when running a single test. I will create and inject my common mocks in the base class TestFixtureSetup method. Then my test class(inheriting the base class) will do the arranging and testing.

The issue is when the method being mocked is called, it acts like the arrange never excuted. I can verify that all the code ran in the order expected, but my mocked methods are just returning nulls.

Below I have a simple example of what I'm trying to explain.


[TestFixture]
public abstract class TestBase
{
    public IClientService ClientService;
 
    [TestFixtureSetUp]
    public void InitFixture()
    {
        ClientService = Mock.Create<IClientService>();
    }
}

[TestFixture]
public class ClientServiceTests : TestBase
{
    [Test]
    public void Test()
    {
        Mock.Arrange(() => ClientService.Retrieve(Arg.AnyLong))
            .Returns(() => new Client());
 
        var client = ClientService.Retrieve(0);
 
        Assert.IsNotNull(client);
    }
}

This does work when the test class has a TestFixtureSetUp of its own and calls the base class setup method, but the base class setup method cannot have the TestFixtureSetUp attribute. Does this mean that the arrange has to execute within the same fixture as the create?
Is there a better way to utilize a base class in this manner? Is this intended?

On a side note, this method was working a couple days ago when I had checked my code in. I will revert my tests to see if i stumbled upon some way to get this to work, I may have had the attributes in just the right places possibly?

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 02 Aug 2013, 12:18 PM
Hi Joshua,

Thank you for reporting this issue.

It appears to be a limitation in JustMock. However, this can be fixed for future releases. Please, visit our Ideas and Feedback and add it as a feature request. This will allow other JustMock clients to vote for its faster implementation and also check the features development status.

To assist you further, I would suggest changing the [TestFixtureSetUp] to [SetUp], like this:
[TestFixture]
public abstract class TestBase
{
    public IClientService ClientService;
     
    [SetUp]
    public void InitFixture()
    {
        ClientService = Mock.Create<IClientService>();
    }
}

[TestFixture]
public class ClientServiceTests : TestBase
{
    [Test]
    public void Test()
    {
        Mock.Arrange(() => ClientService.Retrieve(Arg.AnyLong))
            .Returns(() => new Client());
 
        var client = ClientService.Retrieve(0);
 
        Assert.IsNotNull(client);
    }
}

Doing so, the test behaves as expected.

I hope this helps. Please, let me know if there is anything else I can assist you with.

P.S. As a token of gratitude, we have given you some Telerik points.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
JustMock Free Edition
Asked by
Joshua
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or