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(
It.IsAny<int>(),
It.IsAny<int?>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int>()
);
// ... 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. on the line where the DocumentRepository.GetDocuments() is called from the controller.
Does anyone see any mistake I could have made?
Aucun commentaire:
Enregistrer un commentaire