我有类似的东西要测试:
public void example(ModelView modelView)
{
//...
var statustId= 1;
var requests = _unitOfWork.RequestRepository.Get(m => m.RequestStatusId == statusId);
var requests = _unitOfWork.RequestRepository.Get(m => m.RequestTypeId == modelView.RequestTypeId);
var oldRequests = _unitOfWork.RequestRepository.Get(m => m.Created == DateTime.Now.AddDays(-7));
//...
}
我如何模仿所有" GET"有不同的数据?
这是我到目前为止所做的:不确定表达式应该是什么样子,因为它使用了函数中的变量。
[Test]
public void Test()
{
//Arrange
_requestGenericRepo.Setup(m => m.Get(
It.Is<Expression<Func<Request, bool>>>(e => e.Equals(First GET - using local variable);
_requestGenericRepo.Setup(m => m.Get(
It.Is<Expression<Func<Request, bool>>>(e => e.Equals(Second GET - using variable from modelView));
_requestGenericRepo.Setup(m => m.Get(
It.Is<Expression<Func<Request, bool>>>(e => e.Equals(Third GET));
//Act
var response = _controller.Example( new ModelView
{
RequestTypeId= 1
});
//Assert
...
}
-------------------编辑:-------------------------- -----------------
以上述方式知道如何做会很好,但我可以将所有Get(
更改为Get().Where
,如下所示:
public void example(ModelView modelView)
{
//...
var statustId= 1;
var requests = _unitOfWork.RequestRepository.Get().Where(m => m.RequestStatusId == statusId);
var requests = _unitOfWork.RequestRepository.Get().Where(m => m.RequestTypeId == modelView.RequestTypeId);
var oldRequests = _unitOfWork.RequestRepository.Get().Where(m => m.Created == DateTime.Now.AddDays(-7));
//...
}
[Test]
public void Test()
{
//Arrange
_requestGenericRepo.Setup(m => m.Get(
It.IsAny<Expression<Func<Request, bool>>>());
//Act
var response = _controller.Example( new ModelView
{
RequestTypeId= 1
});
//Assert
...
}
答案 0 :(得分:1)
RequestRepository.Get方法的返回值是多少?您可以尝试使用SetupSequence并选择要测试的路径。类似的东西:
_requestGenericRepo.SetupSequence(m => m.Get(It.IsAny<Expression<Func<Request, bool>>>())
.Returns(/*First call*/)
.Returns(/*Second call*/)
.Returns(/*Third call*/);