Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq Error : Moq.MockVerificationException: The following setups were not matched

I wanna test my method with mock but it throw this exception. My class is this (this class do some simple actions on a file as though unzipping the file) :

public class FileActions
    {
        public virtual void Decompress(FileInfo fileInfo, DirectoryInfo directoryInfo)
        {
            ZipFile.ExtractToDirectory(fileInfo.FullName, directoryInfo.FullName);
        }

        public virtual FileInfo GetConvertedFileToZip(FileInfo fileInfo)
        {
            try
            {
                var changeExtension = Path.ChangeExtension(fileInfo.FullName, "zip");
                File.Move(fileInfo.FullName, changeExtension);
                return new FileInfo(changeExtension);
            }
            catch (Exception)
            {

                throw new FileNotFoundException();
            }

        }
    }

and this is my test :

public void TestMockedMethodForNotNull()
    {
        var mock = new Mock<FileActions>();

        var fInfo = new FileInfo(@"D:\ZipFiles\elmah.nupkg");
        mock.Setup(s => s.GetConvertedFileToZip(fInfo)).Verifiable();
        mock.VerifyAll();
    }

So, why does it get this Error :

Moq.MockVerificationException: The following setups were not matched: FileActions2 s => s.GetConvertedFileToZip(D:\ZipFiles\elmah.nupkg)

like image 779
Amir Jalilifard Avatar asked Dec 14 '13 06:12

Amir Jalilifard


1 Answers

There are several issues with your Unit Test. I will only highlight the mocking side of things, as it relevant to the question you ask. Also your question has refer to "FileActions2", and I think this a mistake when you originally add the question.

You Test:

   [TestMethod]
    public void TestMockedMethodForNotNull()
    {
        var mock = new Mock<FileActions>();

        var fileInfo = new FileInfo(@"D:\ZipFiles\elmah.nupkg");

        mock.Setup(s => s.GetConvertedFileToZip(fileInfo)).Verifiable();

        mock.VerifyAll();
    }

The way you have written this test, Moq won't verify on GetConvertedFileToZip This test fail fundamentally because Moq cannot provide an override for a virtual method GetConvertedFileToZip. You must create a proxy i,e mock.Object.

If you modify your test in such a way so your SUT (Sysytem Under Test), consumes an instance of the mocked object/proxied object your verify would work partially (partially means you are heading right direction). Still something else to fix which I have described below.

Assuming your SUT is like below

public class Sut
{
    public void Do(FileActions fileActions)
    {
        var fileInfo = new FileInfo(@"D:\ZipFiles\elmah.nupkg");
        var s = fileActions.GetConvertedFileToZip(fileInfo);
    }
}

Your Test

    [TestMethod]
    public void TestMockedMethodForNotNull()
    {
        var mock = new Mock<FileActions>();

        var fileInfo = new FileInfo(@"D:\ZipFiles\elmah.nupkg");

        mock.Setup(s => s.GetConvertedFileToZip(fileInfo)).Verifiable();

        var sut = new Sut();

        sut.Do(mock.Object);


        mock.VerifyAll();
    }

This would produce an exception. This is because fileInfo you have setup on does not match the verification, when invoke via the Sut.

If you were to modify this test as below, this would succeed

    [TestMethod]
    public void TestMockedMethodForNotNull()
    {
        var mock = new Mock<FileActions>();

        //var fileInfo = new FileInfo(@"D:\ZipFiles\elmah.nupkg");

        mock.Setup(s => s.GetConvertedFileToZip(It.IsAny<FileInfo>())).Verifiable();

        var sut = new Sut();

        sut.Do(mock.Object);

        mock.VerifyAll();
    }
like image 133
Spock Avatar answered Nov 15 '22 11:11

Spock