C# 无法模拟unitOfWork且没有服务接口

C# 无法模拟unitOfWork且没有服务接口,c#,unit-testing,moq,C#,Unit Testing,Moq,我有以下课程 namespace Foo.Bar.Services { public abstract class Service { public Service(IUnitOfWork unitOfWork) { this.UnitOfWork = unitOfWork; } protected IUnitOfWork UnitOfW

我有以下课程

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.InvalidProxyConstructor Arguments异常:Can 未实例化类的代理:Foo.Bar.Services.ControlService。 找不到无参数构造函数

我已经尝试了一些方法,但我无法让这个以前未经测试的应用程序创建一个要测试的模拟对象

编辑,谢谢。所以我把它改为使用ControlService和mock 1 依赖性。但它的错误在于它不能从 ..组结构到Foo.Bar.i工作单元


通常情况下,被测系统不会被模拟。模拟其依赖项,并将其注入被测类的实例中

[TestFixture]    
public class Control { 

    [Test]    
    public void TestMethod1() {
        //Arrange
        var repository = new Mock<IRepository<GroupStructure>>();
        //...Set up the repository behavior to satisfy logic

        var uow = new Mock<IUnitOfWork>();
        uow.Setup(_ => _.GetRepository<AgencyGroupStructure>())
            .Returns(repository.Object);

        var sut = new ControlService(uow.Object);
        var expected = true;

        //Act
        var actual = sut.HasExternalImage(1249);

        //Assert
        Assert.AreEqual(actual.Item1, expected);    
    }    
}

参考以更好地了解如何使用模拟框架

错误是准确的,因为该类需要iUnitOfWork为什么要开始模拟该类?您通常不会模拟被测试的类。模拟它的依赖关系,并将其注入testWow下的类实例中。你太棒了,让它看起来很简单。这是我多年来一直想要的缺失部分,因为我有一个大型解决方案需要维护并慢慢重写。这太棒了,真是太感谢你了