此问题是作为对 How do you Mock an class for a unit test that has a return type but no input parameters 的跟进发布的
自从提出原始问题后,我现在创建了一个最小、完整且可验证的示例,用作此问题的基础。
我有一个 Controller (如下所示)
public class HomeController : Controller
{
private OrganisationLogic _organisationLogic;
public HomeController(OrganisationLogic logic)
{
_organisationLogic = new OrganisationLogic();
}
public ActionResult Index()
{
var model = _organisationLogic.GetOrganisation().ToViewModel();
return View(model);
}
}
Controller 从名为 OrganisationLogic 的业务逻辑层中的方法检索数据(如下所示)
public class OrganisationLogic : LogicRepository<OrganisationModel>
{
public OrganisationLogic()
{
}
public override OrganisationModel GetOrganisation()
{
return new OrganisationModel { Id = 1, OrganisationName = "My Orgaisation", Address = "Logic" };
}
}
业务逻辑后面继承Logic repository(如下图)
public abstract class LogicRepository<T> : ILogicRepository<T>
{
protected LogicRepository()
{
}
public abstract T GetOrganisation();
}
逻辑存储库实现 ILogicRepository 接口(interface)(如下所示)
public interface ILogicRepository<TModel>
{
TModel GetOrganisation();
}
我想对 HomeController 进行单元测试,以验证 ViewModel 中显示的数据是否从 OrganisationLogic 正确返回并从 OrganisationModel 转换为 OrganisationViewModel。
我编写了以下使用 Moq 模拟方法 _OrganisationLogic.GetOrganisation() 的单元测试。
[TestMethod]
public void Index()
{
var _OrganisationLogic = new Mock<OrganisationLogic>();
var testdata = new OrganisationModel { Id = 3, OrganisationName = "My Test Class Organisation" };
_OrganisationLogic.Setup(p => p.GetOrganisation()).Returns(testdata).Callback<OrganisationModel>(p=>p = testdata);
HomeController controller = new HomeController(_OrganisationLogic.Object);
ViewResult result = controller.Index() as ViewResult;
OrganisationViewModel model = (OrganisationViewModel)result.Model;
Assert.AreEqual(testdata.OrganisationName,model.OrganisationName);
}
当我运行测试时,测试失败了。原因是 Mock 没有覆盖类,而是从业务逻辑层中的实际方法返回结果。
在我最初的问题中,我发布了正在生成的错误消息:
System.ArgumentException 无效回调。具有 0 个参数的方法设置无法调用具有不同数量参数 (1) 的回调。来源 = Moq StackTrace:在 Moq.MethodCallReturn2.ValidateNumberOfCallbackParameters(MethodInfo callbackMethod) 在 Moq.MethodCallReturn2.ValidateReturnDelegate(委托(delegate)回调) 在 Moq.MethodCallReturn2.Returns[T](Func2 valueExpression)
我现在可以通过运行以下单元测试来准确地复制此错误消息。我怀疑上面的单元测试更接近我需要的,并且在下面的实例中我错误地设置了 Return() 。欢迎对此提出想法?
[TestMethod]
public void Index()
{
var _OrganisationLogic = new Mock<OrganisationLogic>();
var testdata = new OrganisationModel { Id = 3, OrganisationName = "My Test Class Organisation" };
_OrganisationLogic.Setup(p => p.GetOrganisation()).Returns<OrganisationModel>(p=>p = testdata).Callback<OrganisationModel>(p=>p = testdata);
HomeController controller = new HomeController(_OrganisationLogic.Object);
ViewResult result = controller.Index() as ViewResult;
OrganisationViewModel model = (OrganisationViewModel)result.Model;
Assert.AreEqual(testdata.OrganisationName,model.OrganisationName);
}
我的问题是如何设置 Mock 以便它使用我的测试数据。
为了帮助回答上述问题,我在 GitHub 上放置了一个版本的代码来演示此问题并显示测试失败。这可以在 https://github.com/stephenwestgarth/UnitTestExample 访问
非常感谢任何帮助。
最佳答案
修改HomeController的构造函数,使参数类型为ILogicRepository<OrganisationModel>
,并且该字段也需要该类型,并使用注入(inject)到构造函数中的实例。 _organisationLogic = logic;
(您上面的代码忽略了参数并创建了自己的 OrganisationLogic 具体实例,这意味着它没有使用您的模拟对象。
在测试中,将 _OrganisationLogic 的声明更改为...
var _OrganisationLogic = new Mock<ILogicRepository<OrganisationModel>>();
正如我之前在您询问时所说的那样,我认为您不需要其中的回调。
编辑后的构造函数看起来像这样......
private ILogicRepository<OrganisationModel> _organisationLogic;
public HomeController(ILogicRepository<OrganisationModel> logic)
{
_organisationLogic = logic;
}
关于c# - 你如何为单元测试最小化一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49244545/