Mock & Initialize Extension Methods

6 posts, 0 answers
  1. Erik
    Erik avatar
    13 posts
    Member since:
    May 2011

    Posted 21 May 2012 Link to this post

    I have a similar issue to these threads:
    http://www.telerik.com/community/forums/justmock/general-discussions/mock-initialize.aspx
    http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

    My issue is with extension methods. I have an extension method for a list called RandomItem. As you could guess, it returns a random item from the list. Now I have a unit test on a function that uses the extension method. So I mock that extension method in the unit test so that I can determine what item comes back so I can verify the expectation. I can run that test by itself all day long and the mock works.

    However, I have another set of tests that actually test the logic of the extension method. When these run with it then the mock on the extension method fails. Generally I would use something like Mock.Initialize or Mock.Partial but it wont even let me do something like this:
    Mock.Initialize<List<ListExtensions>>()
    because it is a static class. So I tried this stuff:

    Mock.SetupStatic(typeof(ListExtensions));
    Mock.Partial<List<IDataItem>>().For(x => x.RandomItem());
     
    But it still fails. Any ideas?
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 24 May 2012 Link to this post

    Hi Eric,

    Thanks again for contacting us.

    Here if you just write the test in the following way, it should work as expected:
    [TestMethod]
    public void ShouldMockExtensions()
    {
        var datatItem = Mock.Create<IDataItem>();
     
        Mock.Arrange(() => datatItem.RandomItem()).Returns(new List<IDataItem> { datatItem  });
     
        Assert.AreEqual(1, datatItem.RandomItem().Count);
    }

    Supporting classes:
    public interface IDataItem
    {
     
    }

    Followed by:
    public static class ListExtensions
    {
        public static List<IDataItem> RandomItem(this IDataItem item)
        {
            return new List<IDataItem>();
        }
    }

    As you can see that you dont need to do an extra initiailzation to do the mock. Hope this answers your question.

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
  4. Erik
    Erik avatar
    13 posts
    Member since:
    May 2011

    Posted 25 May 2012 Link to this post

    Sorry, maybe I wasn't clear. I know how to mock the extension method and it works if the only thing I ever do with it is mock it. The problem is that I have unit tests that actually invoke the extension method. Once it is invoked it can not be mocked in future unit tests.

    Here is the scenario:
    I have test suite A and test suite B. In test suite A I am testing the extension method so I invoke it (don't mock it). In test suite B I am testing code that uses the extension method so I try to mock the extension method. If I run B by itself it works fine. If I run A and B together where A runs before B then B fails because the extension method couldn't be mocked.

    Thanks!
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 30 May 2012 Link to this post

    Hi Eric,

    Thanks again for your reply. 

    Since in test suit A the expected method is invoked and it raises the OnJITCompilationStarted event therefore in suit B, JM profiler fails to intercept it. One way to get around this issue is to initialize it. Here is a post that should give you more insight on this:

    http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

    Kind Regards
    Ricky 
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. Erik
    Erik avatar
    13 posts
    Member since:
    May 2011

    Posted 31 May 2012 Link to this post

    Yes, I mentioned that forum link in my original post. My issue is that I can not initialize a static class. Also, initializing the class that uses the extension method fails. So, how would I initialize an extension method?
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 01 Jun 2012 Link to this post

    Hi Eric,
    Thanks again for your reply.

    In case you want to initialize the ListExtensions class, you can write it in the following way:
    static UnitTest1()
    {
        Mock.Partial(typeof(ListExtensions)).For<IDataItem>((i) => ListExtensions.RandomItem(i));
    }
     
     
    [TestMethod]
    public void TestMethod1()
    {
        ListExtensions.RandomItem(null);
    }
     
    [TestMethod]
    public void ShouldMockExtensions()
    {
        var datatItem = Mock.Create<IDataItem>();
     
        Mock.Arrange(() => datatItem.RandomItem()).Returns(new List<IDataItem> { datatItem });
     
        Assert.AreEqual(1, datatItem.RandomItem().Count);
    }

    I am sending you the project to let you have a look. Please let me know if your test is still failing and in that regard, let me know what else i need to update in the sample project in order to reproduce the problem.


    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top
DevCraft R3 2016 release webinar banner