3

I have something like this:

WebRequestManagerMock
    .Setup(x => x.GetItemsAsync(It.IsAny<IEnumerable<Order>>()))
    .Returns<IEnumerable<Order>>(orders => Task.FromResult<IEnumerabe<Item>>(m_Items.Take(orders.Count())));

m_Items is a static list I filled in the Class Initialize, and is not empty.

Orders is also not empty when I debug the test.

When I call GetItemsAsync I get 0 items no matter how many orders there are.

The mock is also static.

Am I doing anything wrong?

2 Answers 2

2

Apparently I'm blind. I didn't notice I'm creating a new instance of my mock in the TestInitialize method. Thanks for showing me that the problem is in my code and not in moq.

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

Comments

1

I recreated a Minimal, Complete, and Verifiable example of a test for your scenario and was able to get it to pass. Take a look at the following

[TestClass]
public class MoqSetupWithListParameterTests : MiscUnitTests {
    //m_Items is a static list I filled in the Class Initialize, and is not empty. 
    static IEnumerable<Item> m_Items = Enumerable.Range(1, 10).Select(i => new Item());
    //The mock is also static. 
    static Mock<IWebRequestManager> WebRequestManagerMock = new Mock<IWebRequestManager>();

    [TestMethod]
    public void Items_Count_Should_Equal_Orders_Count() {
        //Arrange
        var expected = 3;
        //Orders is also not empty when I debug the test.
        var m_Orders = Enumerable.Range(1, expected).Select(i => new Order());
        WebRequestManagerMock
            .Setup(x => x.GetItemsAsync(It.IsAny<IEnumerable<Order>>()))
            .Returns<IEnumerable<Order>>(orders => Task.FromResult(m_Items.Take(orders.Count())));

        var sut = WebRequestManagerMock.Object;

        //Act
        //When I call GetItemsAsync I get expected count.
        var actual = sut.GetItemsAsync(m_Orders).Result;

        //Assert
        Assert.AreEqual(expected, actual.Count());
    }

    public interface IWebRequestManager {
        Task<IEnumerable<Item>> GetItemsAsync(IEnumerable<Order> enumerable);
    }

    public class Order { }
    public class Item { }
}

5 Comments

Thanks for the help. For some reason, visual studio doesn't recognize async tests, and I can't run them. Other than that, I moved my mock to the test it self, and it kinda works, only now I have a problem with the Returns expression - without the list of orders parameter it works, but not with it(the lambda expression)
@user6251216, edited test so that it is no longer async and still works. note the changes and see it it will recognize the test now. What version of VS are you using and what version of .Net are your targeting
Originally it was Visual Studio 2013 with .NET 4, but I recently upgraded it to 2015 with .NET 4.5. I'm currently having a problem with the Returns<IEnumerable<Order>>(...) - it throws a reflection exception regarding the number of parameters, although it does not seem I have a problem with the parenthesis or something. The normal Returns works though.
Return Task for Async tests; do not use void
@JeroenHeier I had task originally but poster said that their version was not allowing them to use Task with test

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.