8

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..

4

3 回答 3

11

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);
    }
于 2013-01-24T12:36:20.757 回答
6

You can do:

var another = new List<int> { 1 , 2, 3 };
repository.Verify(r => r.Create(It.Is<IEnumerable<int>>(l => l.SequenceEqual(another)));
于 2018-12-06T13:23:23.917 回答
2

Something quick and dirty -

public interface IBlah
{
    void Sum(IEnumerable<int> baz);
}

class Blah : IBlah
{
    public void Sum(IEnumerable<int> baz)
    {
        return;
    }
}

public class Baz
{
    private readonly IBlah blah;

    public Baz(IBlah blah)
    {
        this.blah = blah;
    }

    public void Sum(IEnumerable<int> baz)
    {
        blah.Sum(baz);
    }
}

And Test it like -

[Test]
public void foo()
{
    var mock = new Mock<IBlah>();
    var enumerable = Enumerable.Range(1, 10);

    var baz = new Baz(mock.Object);
    baz.Sum(enumerable.Where(x => x%2 == 0));

    mock.Verify(p => p.Sum(It.Is<IEnumerable<int>>(z => z.All(x => x%2==0))));
}
于 2013-01-24T12:34:48.353 回答