1

I am new to Moq and unit testing in asp net. Let me dive straight into it.

My Test Function is the following:

namespace OfflineMessagingAPI.Tests
{

    public class MessageTests
    {
        [Fact]
        public void ShouldGetAllMessagesSentByAUser()
        {
            // Arrange Data ----
            MessageRequest Request = new MessageRequest();
            Request.From = "userVIP";

            var listOfMessages = new List<AspNetUserMessage>();
            listOfMessages.Add(new AspNetUserMessage {
                From = Request.From,
                To = "User1",
                Text = "DummyTxt"
            });

            listOfMessages.Add(new AspNetUserMessage
            {
                From = "User2",
                To = Request.From,
                Text = "txt2"
            });

            var TranformedList = listOfMessages.AsQueryable();

            // Setup database mocker
            Mock<BlockedUsersAndMessages> mockRepo = new Mock<BlockedUsersAndMessages>();
            mockRepo.Setup(x => x.AspNetUserMessages.Where(o => o.From == Request.From || o.To == Request.From)).Returns(TranformedList);
            var controller = new MessageController(mockRepo.Object);

            // Act ---
            IEnumerable<AspNetUserMessage> messages = controller.GetAllMessages(Request);

            // Assert ---
            Assert.Equal(messages.AsQueryable(), TranformedList));
        }
    }
}

My Where has lambda expression queries as:

Where(o => o.From == Request.From || o.To == Request.From)

because that's how I call it in the GetAllMessages method.

When I run this test I get the following error: Message Error

  Message: 
    System.NotSupportedException : Unsupported expression: ... => ....Where<AspNetUserMessage>(o => o.From == MessageTests.<>c__DisplayClass0_0.Request.From || o.To == MessageTests.<>c__DisplayClass0_0.Request.From)
    Extension methods (here: Queryable.Where) may not be used in setup / verification expressions.
  Stack Trace: 
    Guard.IsOverridable(MethodInfo method, Expression expression)
    InvocationShape.ctor(LambdaExpression expression, MethodInfo method, IReadOnlyList`1 arguments, Boolean exactGenericTypeArguments)
    ExpressionExtensions.<Split>g__Split|4_1(Expression e, Expression& r, InvocationShape& p)
    ExpressionExtensions.Split(LambdaExpression expression)
    Mock.Setup(Mock mock, LambdaExpression expression, Condition condition)
    Mock`1.Setup[TResult](Expression`1 expression)
    MessageTests.ShouldGetAllMessagesSentByAUser() line 41

I can not understand the problem here, any help would be so appreciated!

cs guy
  • 926
  • 2
  • 13
  • 33
  • Does this answer your question? [Moq - Non-overridable members may not be used in setup / verification expressions](https://stackoverflow.com/questions/56905578/moq-non-overridable-members-may-not-be-used-in-setup-verification-expression) – Ian Kemp Apr 12 '20 at 19:52
  • Amazingly, when I plugged that error message into a search engine, the result that came up was the question I linked to. Next time, try doing the same. – Ian Kemp Apr 12 '20 at 19:53
  • Your link does not help a bit and is so your rudeness!! I am trying to mock the entity framework your link has nothing to do with my problem! – cs guy Apr 12 '20 at 20:03
  • https://stackoverflow.com/questions/58671929/unit-testing-for-extension-method-using-moq – Ian Kemp Apr 12 '20 at 20:22

1 Answers1

1

I'm guessing a little bit here as we don't have enough of the schema, but you can't mock the LINQ Where extension directly, you can't mock extension methods.

It sounds like your controller takes a dependency on the repo and you're accessing the AspNetUserMessages property of that repo directly. If that is the case, just setup AspNetUserMessages with the list of existing items to suit the test. The Where invocation in GetAllMessages will work as it normally would.

mockRepo.Setup(x => x.AspNetUserMessages).Returns(TranformedList);

If you want to test that the Where invocation is working as expected, create a test where AspNetUserMessages has existing items that match the Where expression. Then create another that doesn't. Add tests until you're satisfied you've covered the expression.

rgvlee
  • 2,773
  • 1
  • 13
  • 20