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?
Hi Maxim
You can just call the method directly via the 'static' way.
bool result = ExtensionClassName.ContainsElementSubscriptions(mockedMessage.Object);
Just think of it as a regular static method.
Hey Michiel,
Thank you for your answer, but it seems that this does not solve the issue.
I can indeed call it using Qaction.methodname(object), but the failure seems to happen at the ‘setup’ part of the code. In that part, I can’t really use this trick.