繁体   English   中英

未调用模拟方法

[英]Mocked method isn't called

我有测试方法,它在调用模拟方法时失败。

我要测试的控制器:

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;
    }
}

模拟的接口:

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

这是模拟方法的设置和调用:

[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
}

设置严格模式后,我发现未调用源代码问题。 我得到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. 在从控制器调用DocumentRepository.GetDocuments()的行上。

有人看到我可能犯的任何错误吗?

很抱歉留下答案而不是发表评论,但我的声誉不足。 我已经在简单的控制台应用程序中对其进行了测试,并且可以正常工作,因此错误必须在其他地方。

这是要点: https : //gist.github.com/anonymous/78948efcc60bdc64df7e

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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