【问题标题】:unit test fails in edit _get action单元测试在编辑 _get 操作中失败
【发布时间】:2012-11-21 05:21:43
【问题描述】:

我为 edit_get 操作编写了一个单元测试 我的控制器动作是

 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);
       }

控制器动作工作正常。 但是当我编写单元测试时它失败了 我的测试是

[Test]
    public void Edit_Get_ReturnsView()
    {
    //Arrange
    CreateGroupFormModel group = new CreateGroupFormModel()
    {
     GroupId = 2, 
     GroupName = "Test",

     Description = "test" };
     GroupController controller = new GroupController();
    var fake = groupService.GetGroup(2);
    groupRepository.Setup(x => x.GetById(2)).Returns(fake);
    Mapper.CreateMap<CreateGroupFormModel, Group>().ForAllMembers(opt => opt.Ignore());
    Mapper.AssertConfigurationIsValid();
    ViewResult actual = controller.EditGroup(2) as ViewResult;
    Assert.IsNotNull(actual, "View Result is null");
   }

谁能帮帮我。测试失败为

Expected Not Null
actual Null

【问题讨论】:

  • 如何存根 groupService?
  • 您可能还想检查 Mapper.Map(group); 中的映射配置是否正确如果 groupService 返回一个无法映射的组,您可能会得到 null “editGroup”,因此测试失败。

标签: asp.net-mvc unit-testing nunit moq


【解决方案1】:

在您的控制器操作中,您正在调用 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);
    }
}

现在在您的单元测试中,您可以模拟此组服务并提供对 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);
}

【讨论】:

  • 组服务来自存储库,并且该存储库被模拟
  • @RintuMary,在您展示的示例单元测试代码中,情况似乎并非如此。我看不出你在哪里嘲笑了这项服务。请查看我的答案,了解如何实现这一目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
相关资源
最近更新 更多