簡體   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