vendredi 22 janvier 2016

Expected invocation on the mock once, but was 0 times, with Func(T, TResult)

I appear to be having an issue with Mock.Verify that believes a method wasn't called but I can fully verify that it is.

Unit test:

[Test]
public void IterateFiles_Called()
{
     Mock<IFileService> mock = new Mock<IFileService>();
     var flex = new Runner(mock.Object);

     List<ProcessOutput> outputs;
     mock.Verify(x => x.IterateFiles(It.IsAny<IEnumerable<string>>(),
                    It.IsAny<Func<string, ICsvConversionProcessParameter, ProcessOutput>>(),
                    It.IsAny<ICsvConversionProcessParameter>(),
                    It.IsAny<FileIterationErrorAction>(),
                    out outputs), Times.Once);

        }

Runner.cs:

public class Runner
    {
        public Runner(IFileService service)
        {
            string[] paths = new[] {"path1"};

            List<ProcessOutput> output = new List<ProcessOutput>();

            service.IterateFiles(paths, ProcessFile, new CsvParam(), FileIterationErrorAction.ContinueThenThrow, out output);
        }

        protected ProcessOutput ProcessFile(string file, ICsvConversionProcessParameter parameters)
        {
            return new ProcessOutput();
        }
    }

When I debug I can see that service.IterateFiles is being called. In addition as all parameters are marked with It.IsAny<T> the arguments passed don't matter (with the exception of the out parameter - my understand is this cannot be mocked). Yet Moq disagrees the method is called.

Any ideas where I'm going wrong?

Aucun commentaire:

Enregistrer un commentaire