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!

4

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.