Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying an enumerable list in Moq

I'm trying to write a unit test for a method which looks like this:

public int Save(IEnumerable<int> addedIds, IEnumerable<int> removedIds)
{
    var existingIds = repository.Get();
    IEnumerable<int> ids = existingIds.Except(removedIds).Union(addedIds));
    return repository.Create(ids);
}

The test in Moq looks like this:

repository.Setup(r => r.Get()).Returns(CreateList());
service.Save(addedIds, removedIds);
repository.Verify(r => r.Create(It.Is<IEnumerable<int>>(l => VerifyList(l))));

This fails, with this error, and VerifyList() is never called:

Expected invocation on the mock at least once, but was never performed:

r => r.Create(It.Is<IEnumerable'1>(list => VerifyList(list)))

Performed invocations:

IRepo.Create(System.Linq.Enumerable+<UnionIterator>d__88'1[System.Int32])

As the invoked type is not IEnumerable<int> but is in fact System.Linq.Enumerable+<UnionIterator>d__88'1[System.Int32]), the test fails. (Stepping through the test, everything is happening correctly and the results are as expected)

If I call ids.ToList() in the method under test, these are the results:

Expected invocation on the mock at least once, but was never performed:

r => r.Create(It.Is<List'1>(l => VerifyList(l)))

Performed invocations: IRepo.Create(System.Collections.Generic.List'1[System.Int32])

Is there any way round this? Or am I doing something wrong?

Edit: it turns out I had a mistake in my VerifyList method so it was returning false, but Moq wasn't giving me that information. The type difference is a red herring..

like image 633
stuartd Avatar asked Jan 24 '13 12:01

stuartd


1 Answers

This seems to work. Made some assumptions though. Guess the VerifyList method could be better. =)

[Test]
    public void Test()
    {
        // SETUP
        Mock<IRepository> repository = new Mock<IRepository>();
        Service service = new Service(repository.Object);
        repository.Setup(r => r.Get()).Returns(CreateList());

        IEnumerable<int> addedIds = new[]{1,2};
        IEnumerable<int> removedIds = new[]{3,4};
        service.Save(addedIds, removedIds);

        repository.Verify(r => r.Create(It.Is<IEnumerable<int>>(l => VerifyList(l))));
    }

    private static bool VerifyList(IEnumerable<int> enumerable)
    {
        return enumerable.Contains(1) && enumerable.Contains(2) && enumerable.Contains(5);
    }

    private IEnumerable<int> CreateList()
    {
        return new[] { 3, 4, 5 };
    }

    public interface IRepository
    {
        IEnumerable<int> Get();
        int Create(IEnumerable<int> id);
    }
    public class Service
    {
        public Service(IRepository repository)
        {
            this.repository = repository;
        }

        private IRepository repository;

        public int Save(IEnumerable<int> addedIds, IEnumerable<int> removedIds)
    {
        var existingIds = repository.Get();
            IEnumerable<int> ids = existingIds.Except(removedIds).Union(addedIds);

        return repository.Create(ids);
    }
like image 73
Markus Avatar answered Oct 24 '22 22:10

Markus