模拟方法未被调用
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.
在从控制器调用 DocumentRepository.GetDocuments()
的行上。
有人看到我可能犯的任何错误吗?
很抱歉留下答案而不是评论,但我没有足够的声誉。
我已经在简单的控制台应用程序中测试过它并且它可以工作,所以错误一定是在其他地方。
这里是要点:https://gist.github.com/anonymous/78948efcc60bdc64df7e
我有测试方法,但调用模拟方法失败。
我要测试的控制器:
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