我尝试为Api控制器编写单元测试。这是一个C#mvc应用程序。单元测试也适用于Moq。
这是控制器:
public sealed class CaseController : ApiControllerBase
{
[HttpGet]
[Route("{id:int}")]
public ICaseDto<object> Get(int id)
{
return _caseProcess.GetCase(id);
}
}
这是接口:
public interface ICaseProcess
{
ICaseDto<object> GetCase(int id);
}
这是方法:
internal sealed class CaseProcess : ProcessBase, ICaseProcess
{
public ICaseDto<object> GetCase(int id)
{
var caseEntity = GetCaseById(id);
CaseProcessHelper helper = GetHelper(caseEntity);
object details = helper.GetReferenceDetails();
ICaseDto<object> resultDto = CaseDto.Create(details);
Mapper.Map(caseEntity, resultDto);
return resultDto;
}
}
我尝试这样编写单元测试:
[TestMethod]
public void CaseConroller_ReturnDetailData_Test()
{
//Arrange
var dto = new Case();
var mockService = new Mock<ICaseProcess>();
var Casecontroller = new CaseController(ICaseDto<Case>);
var expected = new CaseEditDto();
//var dataSource = new
CaseProcess.Setup(a => a.Should(dto))
//Act
// var result = "hallo";
//Assert
result.Should().BeEquivalentTo();
}
那么您到底需要编写单元测试什么?
我有一个详细视图,然后Api必须返回该详细视图的数据。
id是一个整数,例如:api/support/cases/100000002
。
构造函数如下:
public CaseController(ICaseProcess caseProcess)
{
_caseProcess = caseProcess;
}
我现在这样子:
//Arrange
var dto = new Case();
var mockService = new Mock<ICaseProcess>();
var casecontroller = new CaseController(CaseProcess.Object);
var expected = new CaseEditDto();
//Act
var result = casecontroller.Get(100000001);
//Assert
result.Should().BeEquivalentTo(expected);
但是输出是这样的:
{
"details": {
"name": "Val van Urquhart",
"dateOfBirth": "11 september 1933"
},
"dateSubmittedFormatted": "1 januari 2018",
"dateClosedFormatted": null,
"sample": false,
"comments": [
{
"id": 1,
"account": "todo@local.nl",
"date": "08-11-2018 17:13",
"text": "ABC"
}
],
"id": 100000001,
"status": 103,
"substatus": null,
"verdict": null,
"owner": 101,
"dateSubmitted": "01-01-2018",
"dateClosed": null,
"type": 100,
"reference": "123459850"
}
我这样尝试:
[TestMethod]
public void CaseController_GetCase_Test()
{
var CaseObject = new CaseDto<object>()
{
Id = 100000001,
Verdict = null,
DateSubmitted = "01-01-2018",
DateClosed = null,
Reference = "123459850"
};
var CaseTest = new CaseEditDto<object>();
// Case entity = UnitOfWork.GetRepository<Case>()
}
答案 0 :(得分:2)
根据显示的代码段,假设控制器看起来像
public sealed class CaseController : ApiControllerBase {
private readonly ICaseProcess caseProcess;
public CaseController(ICaseProcess caseProcess) {
this.caseProcess = caseProcess;
}
[HttpGet]
[Route("{id:int}")]
public ICaseDto<object> Get(int id) {
return caseProcess.GetCase(id);
}
}
一个非常简单的测试是模拟依赖关系,将其注入被测对象,进行测试,然后断言预期的行为
[TestMethod]
public void CaseConroller_ReturnDetailData_Test() {
//Arrange
//mock the dependency
var id = 100000001;
var expected = Mock.Of<ICaseDto<object>>();
var mockService = new Mock<ICaseProcess>();
mockService.Setup(_ => _.GetCase(id)).Returns(expected);
//inject it into the subject under test
var subject = new CaseController(mockService.Object);
//Act
var actual = subject.Get(id); //exercise the test
//Assert
//assert the expected behavior
actual.Should().BeEquivalentTo(expected);
}