Create Mocks in a Testing Base Class

2 posts, 0 answers
  1. Joshua
    Joshua avatar
    1 posts
    Member since:
    Nov 2012

    Posted 31 Jul 2013 Link to this post

    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?
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 02 Aug 2013 Link to this post

    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.
  3. DevCraft R3 2016 release webinar banner
Back to Top