新使用FakeItEasy。在
处出现错误A.CallTo(() => Lister.GetResult(A<int>._, A<int>._)).MustHaveHappened();
所以我猜这个
A.CallTo(() => Lister
.GetResult(A<int>._, A<int>._))
.Returns(new ResponseList(1, new List<RecordsValues>())) ;
未被触发。当我删除. musthavehappened断言时,测试继续进行,也获得200 statuscode(控制器在测试中)。它传递上面的第二个代码片段,但不会抛出错误。
请多多指教。
测试
public class GetByListTests
{
[Fact]
public void GetByList_GetFieldsByRange_Test()
{
ILister Lister = A.Fake<ILister>();
IDbConnectionFactory dbConnectionFactory = A.Fake<IDbConnectionFactory>();
GetByList GetByList = new(dbConnectionFactory);
A.CallTo(() => Lister
.GetResult(A<int>._, A<int>._))
.Returns(new ResponseList(1, new List<RecordsValues>()));
//result contains empty and 0 values, with 200 statuscode
//result it does not contain the expected return declared on the above callTo return
var result = (OkObjectResult)GetByList.GetFieldsByRange();
//if I remove this, the test passes but with incorrect result value, so I assume it really is not being called as when I add the line below, it errs out
A.CallTo(() => Lister.GetResult(A<int>._, A<int>._)).MustHaveHappened();
Assert.NotNull(result);
Assert.Equal(200, result.StatusCode);
}
}
端点
private readonly ILister Lister;
public GetByList(IDbConnectionFactory dbConnectionFactory)
{
DataService securityProjectDataService = new(dbConnectionFactory);
Lister = new SecurityProjectLister(DataService);
}
[HttpGet]
public IActionResult GetFieldsByRange([FromQuery][Range(0, int.MaxValue)] int skip = 0, [FromQuery][Range(0, 1000)] int top = 1000)
{
ResponseList response = securityProjectLister.GetResult(skip, top);
return Ok(response);
}
如果我缺少什么,请指出来。我很乐意吸取任何教训。谢谢大家,干杯!我认为主要问题是你从来没有真正使用ILister
假。
您创建的假文件位于您的测试方法
ILister Lister = A.Fake<ILister>();
而在端点类中创建单独的ILister
对象:
Lister = new SecurityProjectLister(DataService);
为了真正受益于您的ILister
假,您需要在您的端点类中对ILister
对象进行依赖注入。
目前,我发现端点类片段有点难以理解。我相信有些名字被混淆了,所以当前的实现:
private readonly ILister Lister;
public GetByList(IDbConnectionFactory dbConnectionFactory)
{
DataService securityProjectDataService = new(dbConnectionFactory);
Lister = new SecurityProjectLister(DataService);
}
[HttpGet]
public IActionResult GetFieldsByRange(...)
{
ResponseList response = securityProjectLister.GetResult(skip, top);
return Ok(response);
}
实际上应该是这样的:
private readonly ILister securityProjectLister; // EDITED
public GetByList(IDbConnectionFactory dbConnectionFactory)
{
DataService securityProjectDataService = new(dbConnectionFactory);
securityProjectLister = new SecurityProjectLister(securityProjectDataService); // EDITED
}
[HttpGet]
public IActionResult GetFieldsByRange(...)
{
// Now, your ILister object is used here:
ResponseList response = securityProjectLister.GetResult(skip, top);
return Ok(response);
}
如果是这种情况,我实际上认为您需要将DataService
对象和ILister
对象的创建提取到端点类的外部,并为端点类构造函数提供一个ILister
对象。
我相信一个可能的实现类似于以下:
端点类片段:
private readonly ILister securityProjectLister;
public GetByList(ILister lister)
{
securityProjectLister = lister;
}
[HttpGet]
public IActionResult GetFieldsByRange(...)
{
// Remains unchanged
}
测试方法:
public void GetByList_GetFieldsByRange_Test()
{
IDbConnectionFactory dbConnectionFactory = A.Fake<IDbConnectionFactory>();
DataService securityProjectDataService = new(dbConnectionFactory);
ILister Lister = new SecurityProjectLister(securityProjectDataService);
GetByList GetByList = new(Lister);
// Unchanged:
A.CallTo(() => Lister
.GetResult(A<int>._, A<int>._))
.Returns(new ResponseList(1, new List<RecordsValues>()));
// The remaining code remains unchanged
}
现在,GetByList
对象正在使用您在测试方法中配置了A.CallTo(...).Returns(...);
的ILister
对象。