简体   繁体   中英

Mocked method isn't called

I have test method and it fails on calling mocked method.

The controller I want to test:

public class DocumentsController : BaseController
{
    public IDocumentRepository DocumentRepository { get; private set; }

    public DocumentsController(IDocumentRepository documentRepository)
    {
        DocumentRepository = documentRepository;
    }

    public Documents GetDocuments(int projectPK, int? folderPK, string search, int page, int pageSize)
    {
        Documents documents =
            DocumentRepository.GetDocuments(
                projectPK,
                folderPK,
                true,
                search,
                UserPK, //saved in HttpConfiguration
                page,
                pageSize,
                CustomerConnectionString //saved in HttpConfiguration
            );

        return documents;
    }
}

The mocked interface:

public interface IDocumentRepository
{
    Documents GetDocuments(
        int projectPK,
        int? folderPK,
        bool useFolders,
        string search,
        int user,
        int page, 
        int pageSize,
        string customerConnectionString);
}

And this is the setup and call of mocked method:

[TestMethod]
public void GetDocuments_ActionExecutes_ReturnsDocuments()
{
    Mock<IDocumentRepository> repositoryMock =
        new Mock<IDocumentRepository>(MockBehavior.Strict);
            repositoryMock        
                .Setup(
                    c => c.GetDocuments(
                            It.IsAny<int>(),
                            It.IsAny<int?>(),
                            It.IsAny<bool>(),
                            It.IsAny<string>(),
                            It.IsAny<int>(),
                            It.IsAny<int>(),
                            It.IsAny<int>(),
                            It.IsAny<string>()
                        )
                )
                .Returns(new Documents());

    documentsController = new DocumentsController(repositoryMock.Object);
    documentsController.InitHttpConfiguration();

    Documents response =
            documentsController.GetDocuments(
                    projectPK: 1,
                    folderPK: 1,
                    search: "",
                    page: 1,
                    pageSize: 10
            );

    // ... Asserts
}

After setting strict mode I've found out the source problem is not called. I get Moq.MockException: IDocumentRepository.GetDocuments(0, null, True, null, 0, 1, 3, "") invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup. Moq.MockException: IDocumentRepository.GetDocuments(0, null, True, null, 0, 1, 3, "") invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup. on the line where the DocumentRepository.GetDocuments() is called from the controller.

Does anyone see any mistake I could have made?

Sorry for leaving answer instead of comment, but I've no enough reputation. I have tested that in simple console application and it works, so the error must be somewhere else.

Here is the gist: https://gist.github.com/anonymous/78948efcc60bdc64df7e

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM