我正在测试一个控制器。
那是我的控制者:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Resources;
using System.Security.AccessControl;
using Pmanager.DAL;
namespace Pmanager.Models
{
public enum Permission
{
Not = 0, Read = 1, Analyse = 2, Edit = 3, Admin = 4
}
public class Group : IGroup
{
public int ID { get; set; }
[Required]
[StringLength(50)]
[Index(IsUnique = true)]
public string Name { get; set; }
[Required]
public bool IsOperations { get; set; }
public Permission? OperationPermission { get; set; }
public virtual ICollection<UserGroup> UserGroups { get; set; }
public int NewGroup(Group group)
{
RioBravoManagerContext ctx = new RioBravoManagerContext();
ctx.Groups.Add(group);
ctx.SaveChanges();
return group.ID;
}
public void UpdateGroup(Group group)
{
RioBravoManagerContext ctx = new RioBravoManagerContext();
if (ctx.Groups.Where(g => g.ID == group.ID).Any())
{
Group cgroup = ctx.Groups.Where(g => g.ID == group.ID).SingleOrDefault();
cgroup.Name = group.Name;
cgroup.IsOperations = group.IsOperations;
cgroup.OperationPermission = group.OperationPermission;
}
else
{
ctx.Groups.Add(group);
}
ctx.SaveChanges();
}
}
public interface IGroup
{
void UpdateGroup(Group group);
int NewGroup(Group group);
}
}
这是我的模特:
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pmanager.Controllers;
using Pmanager.Tests.Helpers;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using Pmanager.Models;
using System;
using Moq;
namespace Pmanager.Tests.Controllers
{
[TestClass]
public class GroupControllerTest
{
JavaScriptSerializer m_serializer;
ControllerContext m_context;
[TestInitialize]
public void TestInitialize()
{
m_serializer = new JavaScriptSerializer();
m_context = TestModelHelper.AdminControllerContext();
}
public void GroupController_UpdateGroup_Valid()
{
// Arrange
GroupController controller = new GroupController();
controller.ControllerContext = m_context;
var _group = new Mock<IGroup>();
_group.Setup(g => g.UpdateGroup(It.IsAny<Group>())).Callback((Group group) => { });
controller.p_group = _group.Object as Group;
Group _gp = new Group
{
ID = 1,
Name = "Group Name",
IsOperations = false,
OperationPermission = Permission.Read
};
// Act
var result = (JsonResult)controller.UpdateGroup(_gp);
JsonGroup resultFund = m_serializer.Deserialize<JsonGroup>(m_serializer.Serialize(result.Data));
// Assert
Assert.IsNotNull(result.Data, "There should be some data for the JsonResult");
Assert.IsNotNull(resultFund.status, "JSON record does not contain 'status' required property.");
Assert.IsTrue(resultFund.status.Equals("success"), "status must be 'success'");
}
[TestMethod]
public void GroupController_UpdateGroup_Invalid()
{
// Arrange
GroupController controller = new GroupController();
controller.ControllerContext = m_context;
var _group = new Mock<IGroup>();
_group.Setup(g => g.UpdateGroup(It.IsAny<Group>())).Callback((Group group) => { });
controller.p_group = _group.Object as Group;
controller.ModelState.AddModelError("Name", @"Missing Name");
Group _gp = new Group
{
ID = 1,
IsOperations = false,
OperationPermission = Permission.Read
};
// Act
var result = (JsonResult)controller.UpdateGroup(_gp);
JsonGroup resultFund = m_serializer.Deserialize<JsonGroup>(m_serializer.Serialize(result.Data));
// Assert
Assert.IsNotNull(result.Data, "There should be some data for the JsonResult");
Assert.IsNotNull(resultFund.status, "JSON record does not contain 'status' required property.");
Assert.IsTrue(resultFund.status.Equals("fail"), "status must be 'fail'");
}
[TestMethod]
public void GroupController_NewGroup_Valid()
{
// Arrange
GroupController controller = new GroupController();
controller.ControllerContext = m_context;
var _fakegroup = new Mock<IGroup>() { CallBase = false };
_fakegroup.Setup(g => g.NewGroup(It.IsAny<Group>())).Returns<Group>((group) => { return 0; }).Verifiable();
controller.p_group = _fakegroup.Object as Group;
Group _gp = new Group
{
ID = 0,
Name = "Group Name",
IsOperations = false,
OperationPermission = Permission.Read
};
// Act
var result = (JsonResult)controller.NewGroup(_gp);
JsonGroup resultFund = m_serializer.Deserialize<JsonGroup>(m_serializer.Serialize(result.Data));
// Assert
Assert.IsNotNull(result.Data, "There should be some data for the JsonResult");
Assert.IsNotNull(resultFund.status, "JSON record does not contain 'status' required property.");
Assert.IsTrue(resultFund.status.Equals("success"), "status must be 'success'");
}
[TestMethod]
public void GroupController_NewGroup_Invalid()
{
// Arrange
GroupController controller = new GroupController();
controller.ControllerContext = m_context;
var _group = new Mock<IGroup>();
_group.Setup(g => g.NewGroup(It.IsAny<Group>())).Returns((Group group) => { return 0; });
controller.p_group = _group.Object as Group;
controller.ModelState.AddModelError("Name", @"Missing Name");
Group _gp = new Group
{
ID = 1,
IsOperations = false,
OperationPermission = Permission.Read
};
// Act
var result = (JsonResult)controller.NewGroup(_gp);
JsonGroup resultFund = m_serializer.Deserialize<JsonGroup>(m_serializer.Serialize(result.Data));
// Assert
Assert.IsNotNull(result.Data, "There should be some data for the JsonResult");
Assert.IsNotNull(resultFund.status, "JSON record does not contain 'status' required property.");
Assert.IsTrue(resultFund.status.Equals("fail"), "status must be 'fail'");
}
}
public class JsonGroup
{
public string status { get; set; }
public List<SimpleGroup> groups { get; set; }
}
}
测试类是:
var _fakegroup = new Mock<IGroup>() { CallBase = false };
_fakegroup.Setup(g => g.NewGroup(It.IsAny<Group>())).Returns<Group>((group) => { return 0; }).Verifiable();
问题是:方法上的模拟不起作用!它在调用时运行实际方法。基本上是:
onTokenRefresh()
我该怎么办?
答案 0 :(得分:3)
您的课程应该承担一项责任。改变的一个原因。一个共同的工作。该示例看起来像是使用Group
作为模型/ DTO和提供函数/方法的服务。
将功能剥离到自己的关注中。
public interface IGroupService {
void UpdateGroup(Group group);
int NewGroup(Group group);
}
public class GroupService : IGroupService {
public int NewGroup(Group group) {
RioBravoManagerContext ctx = new RioBravoManagerContext();
ctx.Groups.Add(group);
ctx.SaveChanges();
return group.ID;
}
public void UpdateGroup(Group group) {
RioBravoManagerContext ctx = new RioBravoManagerContext();
if (ctx.Groups.Where(g => g.ID == group.ID).Any()) {
Group cgroup = ctx.Groups.Where(g => g.ID == group.ID).SingleOrDefault();
cgroup.Name = group.Name;
cgroup.IsOperations = group.IsOperations;
cgroup.OperationPermission = group.OperationPermission;
} else {
ctx.Groups.Add(group);
}
ctx.SaveChanges();
}
}
保持模特精益。他们唯一的责任是保留要转移的信息。
public enum Permission {
Not = 0, Read = 1, Analyse = 2, Edit = 3, Admin = 4
}
public class Group {
public int ID { get; set; }
[Required]
[StringLength(50)]
[Index(IsUnique = true)]
public string Name { get; set; }
[Required]
public bool IsOperations { get; set; }
public Permission? OperationPermission { get; set; }
public virtual ICollection<UserGroup> UserGroups { get; set; }
}
让控制器依赖服务并通过构造函数注入它。
[Authorize]
public class GroupController : DefaultController {
private readonly IGroupService groupService;
public GroupController(IGroupService groupService) {
this.groupService = groupService;
}
[HttpPost]
[AllowAdminOperationsOnly]
public ActionResult NewGroup(Group _group) {
try {
int id_group = 0;
if (ModelState.IsValid) {
id_group = groupService.NewGroup(_group);
} else {
ThrowErrorMessages();
}
return Json(new
{
status = "success",
title = @Resources.Global.success,
text = @Resources.Groups.success_creating_group,
messageType = "success",
id_group = id_group
}, JsonRequestBehavior.AllowGet);
} catch (Exception err) {
return ErrorHelper(@Resources.Global.error, @Resources.Groups.error_creating_group, err.Message);
}
}
}
对于给定的测试方法
,不能单独进行测试[TestMethod]
public void GroupController_NewGroup_Valid() {
// Arrange
var _fakegroup = new Mock<IGroupService>();
_fakegroup.Setup(g => g.NewGroup(It.IsAny<Group>())).Returns(1).Verifiable();
var controller = new GroupController(_fakegroup.Object);
var _gp = new Group {
ID = 0,
Name = "Group Name",
IsOperations = false,
OperationPermission = Permission.Read
};
// Act
var result = controller.NewGroup(_gp) as JsonResult;
// Assert
_fakegroup.Verify();
Assert.IsNotNull(result.Data, "There should be some data for the JsonResult");
dynamic data = result.Data;
Assert.IsNotNull(data.status, "JSON record does not contain 'status' required property.");
Assert.IsTrue(data.status.Equals("success"), "status must be 'success'");
}
最后记得用DI容器注册服务抽象和实现。
答案 1 :(得分:1)
将控制器中的属性从Group
类型更改为IGroup
。
private IGroup m_group;
public IGroup p_group
{
get
{
if (m_group == null)
{
m_group = new Group();
}
return m_group;
}
set
{
m_group = value;
}
}
在测试中:
controller.p_group = _fakegroup.Object;
这将解决您的问题。您在控制器中使用依赖项作为具体类型而不是抽象(接口)。