Hey Dojo-community,
I was working on some unit tests and was wondering what the prefered method for solving the following issue is:
imagine I have three extension methods:
public static bool ContainsElementSubscriptions(this SubscriptionSetInfoMessage message)
public static bool ContainsServiceSubscriptions(this SubscriptionSetInfoMessage message)
public static bool ContainsServiceAndElementSubscriptions(this SubscriptionSetInfoMessage message)
The last extension method uses the first two to return a result.
I would like to cover each of these methods in a test, but I don't want them to be dependant on each other, to make it clear which method exactly fails.
To do this, I did the following:
Mock<SubscriptionSetInfoMessage> mockedMessage = new Mock<SubscriptionSetInfoMessage>();
mockedMessage.CallBase = true;
mockedMessage.Setup(x => x.ContainsElementSubscriptions()).Returns(true);
mockedMessage.Setup(x => x.ContainsServiceSubscriptions()).Returns(true);
When writing the unit test for the last method, I got this error when trying to mock the first two methods:
Tests.QActionTests.ContainsServiceAndElementSubscriptionsTest threw exception:
System.NotSupportedException: Unsupported expression: x => x.ContainsElementSubscriptions()
Extension methods (here: QAction.ContainsElementSubscriptions) may not be used in setup / verification expressions.
How would you solve this?
There are probably better solutions but I came across a similar situation once and there I found you could use the Moles framework for mocking static and built-in .NET methods.