未调用模拟方法
本文关键字:方法 模拟 调用 | 更新日期: 2023-09-27 18:05:07
我有一个测试方法,它在调用模拟方法时失败。
我要测试的控制器:
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.
在从控制器调用DocumentRepository.GetDocuments()
的那一行。
有没有人看到我可能犯的错误?
很抱歉留下回复而不是评论,但我没有足够的声誉。我已经在简单的控制台应用程序中测试过了,它可以工作,所以错误一定是在其他地方。
要点如下:https://gist.github.com/anonymous/78948efcc60bdc64df7e