Asp.net mvc 单元测试在编辑\u获取操作中失败
我为edit\u get操作编写了一个单元测试 我的控制器操作是Asp.net mvc 单元测试在编辑\u获取操作中失败,asp.net-mvc,unit-testing,nunit,moq,Asp.net Mvc,Unit Testing,Nunit,Moq,我为edit\u get操作编写了一个单元测试 我的控制器操作是 public class GroupController : Controller { private readonly IGroupService groupService; public GroupController(IGroupService groupService) { this.groupService = groupService; } pu
public class GroupController : Controller
{
private readonly IGroupService groupService;
public GroupController(IGroupService groupService)
{
this.groupService = groupService;
}
public ActionResult EditGroup(int id)
{
var group = groupService.GetGroup(id);
CreateGroupFormModel editGroup = Mapper.Map<Group, CreateGroupFormModel>(group);
if (group == null)
{
return HttpNotFound();
}
return View("_EditGroup", editGroup);
}
在控制器操作中,您正在调用
var group=groupService.GetGroup(id)
这个groupService
的来源还不是很清楚。在单元测试中,您必须模拟它。为了实现这一点,您的GroupController
必须将此依赖项作为构造函数注入
此外,在单元测试中,您似乎声明了一些从未使用过的group
变量
例如:
public class GroupController: Controller
{
private readonly IGroupService groupService;
public GroupController(IGroupService groupService)
{
this.groupService = groupService;
}
public ActionResult EditGroup(int id)
{
var group = this.groupService.GetGroup(id);
CreateGroupFormModel editGroup = Mapper.Map<Group, CreateGroupFormModel>(group);
if (group == null)
{
return HttpNotFound();
}
return View("_EditGroup", editGroup);
}
}
公共类GroupController:Controller
{
专用只读IGroupService-groupService;
公共GroupController(IGroupService groupService)
{
this.groupService=groupService;
}
公共操作结果编辑组(int id)
{
var group=this.groupService.GetGroup(id);
CreateGroupFormModel editGroup=Mapper.Map(组);
如果(组==null)
{
返回HttpNotFound();
}
返回视图(“编辑组”,编辑组);
}
}
现在,在单元测试中,您可以模拟此组服务,并提供对GetGroup方法结果的预期:
[Test]
public void Edit_Get_ReturnsView()
{
// arrange
var group = new CreateGroupFormModel
{
GroupId = 2,
GroupName ="Test",
Description ="test"
};
var groupServiceMock = new Mock<IGroupService>();
groupServiceMock.Setup(x => x.GetGroup(group.GroupId)).Returns(group);
var sut = new GroupController(groupServiceMock.Object);
Mapper.CreateMap<CreateGroupFormModel, Group>().ForAllMembers(opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
// act
var actual = sut.EditGroup(group.GroupId) as ViewResult;
// assert
Assert.IsNotNull(actual);
Assert.IsInstanceOfType(typeof(ViewResult), actual);
}
[测试]
公共无效编辑\u获取\u返回视图()
{
//安排
变量组=新建CreateGroupFormModel
{
GroupId=2,
GroupName=“测试”,
Description=“测试”
};
var groupServiceMock=new Mock();
groupServiceMock.Setup(x=>x.GetGroup(group.GroupId)).Returns(group);
var sut=新的GroupController(groupServiceMock.Object);
用于所有成员(opt=>opt.Ignore());
assertConfigurationsValid();
//表演
var actual=sut.EditGroup(group.GroupId)作为ViewResult;
//断言
Assert.IsNotNull(实际值);
Assert.IsInstanceOfType(typeof(ViewResult),实际值);
}
您是如何删除groupService的?您可能还需要检查Mapper.Map(group)中的映射配置是否正确;如果groupService返回一个无法映射的组,您可能会得到null“editGroup”,因此test fail.group服务来自一个存储库,并且此存储库为空mocked@RintuMary,在您展示的示例单元测试代码中,情况似乎并非如此。我看不出你在哪里嘲笑这项服务。请看我的答案,以了解如何实现这一目标。
public class GroupController: Controller
{
private readonly IGroupService groupService;
public GroupController(IGroupService groupService)
{
this.groupService = groupService;
}
public ActionResult EditGroup(int id)
{
var group = this.groupService.GetGroup(id);
CreateGroupFormModel editGroup = Mapper.Map<Group, CreateGroupFormModel>(group);
if (group == null)
{
return HttpNotFound();
}
return View("_EditGroup", editGroup);
}
}
[Test]
public void Edit_Get_ReturnsView()
{
// arrange
var group = new CreateGroupFormModel
{
GroupId = 2,
GroupName ="Test",
Description ="test"
};
var groupServiceMock = new Mock<IGroupService>();
groupServiceMock.Setup(x => x.GetGroup(group.GroupId)).Returns(group);
var sut = new GroupController(groupServiceMock.Object);
Mapper.CreateMap<CreateGroupFormModel, Group>().ForAllMembers(opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
// act
var actual = sut.EditGroup(group.GroupId) as ViewResult;
// assert
Assert.IsNotNull(actual);
Assert.IsInstanceOfType(typeof(ViewResult), actual);
}