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

Mock & Initialize Extension Methods

5 Answers 135 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Erik
Top achievements
Rank 1
Erik asked on 21 May 2012, 10:14 PM
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?

5 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 24 May 2012, 06:44 PM
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 >>

0
Erik
Top achievements
Rank 1
answered on 25 May 2012, 03:37 PM
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!
0
Ricky
Telerik team
answered on 30 May 2012, 06:43 PM
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 >>

0
Erik
Top achievements
Rank 1
answered on 31 May 2012, 04:05 PM
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?
0
Ricky
Telerik team
answered on 01 Jun 2012, 07:35 PM
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 >>

Tags
General Discussions
Asked by
Erik
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Erik
Top achievements
Rank 1
Share this question
or