【发布时间】:2018-09-03 05:53:12
【问题描述】:
我有以下课程
namespace Foo.Bar.Services
{
public abstract class Service
{
public Service(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}
protected IUnitOfWork UnitOfWork { get; private set; }
}
}
using...
namespace Foo.Bar.Services
{
public class ControlService : Service
{
...
private readonly IRepository<GroupStructure> groupStructures = null;
public ControlService(IUnitOfWork uow) : base(uow)
{
...
this.agencyGroupStructures = this.UnitOfWork.GetRepository<AgencyGroupStructure>();
}
public Tuple<bool, int> HasExternalImage(int branchId)
{
var externalResultList = from a in this.Structures.Table
where a.GroupID == branch.GroupID
&& (a.AreExternalRequired == true)
&& (a.ProductTypeID == ETourType.Trailer)
&& !a.Deleted
select a;
return (some logic based on above...)
}
}
测试
namespace ControlTests
{
[TestFixture]
public class Control
{
//unable to create service due to being abstact
[Test]
public void TestMethod1()
{
******Changed here******
var Mock = new Mock<GroupStructureService> { CallBase = true };
var fakeControl = new ControlService(Mock.Object)
var sut = fakeControl.HasExternalImage(1249);
Assert.That(sut.Item1, "true");
}
}
}
使用 NUnit 和 Moq 运行上述命令会显示以下消息:
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : 可以 不实例化类的代理:Foo.Bar.Services.ControlService.
找不到无参数构造函数。
我已经尝试了一些方法,但我无法让这个以前未经测试的应用程序创建一个模拟对象来测试
编辑,谢谢。所以我把它改成使用 ControlService 和 mock 1 依赖。但它的错误是它不能从 ....GroupStructure 到 Foo.Bar.IUnitOfWork
【问题讨论】:
-
错误是准确的,因为类需要
IUnitOfWork -
你为什么要嘲笑那堂课?您通常不会模拟被测类。模拟其依赖项并将其注入到被测类的实例中
标签: c# unit-testing moq