Tag: moq

如何防止使用Moq调用基类构造函数?

如何防止使用Moq调用基类构造函数? 我无法用Moq模拟一个对象,因为正在调用基类构造函数并且它需要真正的对象,所以我想停止调用基类构造函数。 var parametersMoq = new Mock(null, “Params”, null){ CallBase = false, }; _storedProcedureAccessor._parameters = parametersMoq.Object; MyDerivedClass的基类构造函数导致了我的问题。

用Mock测试Nunit。 接口实例

我有以下(简化)代码。 public class Controller { private readonly IService _service; public Controller(IService service) { _service = service; } public async Task Create(MyObject object) { var result = _service.method(object); if (!result.Succeeded) { return this.GetErrorResult(object); } } } 和SimpleInjector用于注入_service和它的实现类之间的依赖关系,如下所示: public static void Register(Container container) { container.Register(); } 作为一个注释,注射和unit testing对我来说是新的,所以我不完全理解它们,但我正在学习。 如果我通过Swagger运行应用程序,一切正常。 注意,当我通过Swagger运行应用程序时,会调用Register函数。 现在,我正在尝试使用NUnit设置一些unit testing,并且像这样模拟IService对象: var Service = new Mock(); […]

将Autofac和Moq与Delegate Factories一起使用

我正在尝试对使用工厂注入的类进行unit testing。 我有一个类,它实例化同一个对象的两个副本(使用不同的配置)来控制硬件。 我正在尝试在模拟硬件调用时测试类的行为。 我已经在构造函数中注入了一组工厂委托,以便类可以根据需要实例化硬件类。 但是,我无法解决如何在Autofac.Extras.Moq包中控制或创建工厂方法。 程序包似乎不支持此function。 我正在寻找一个等效的电话: mock.Provide(//created by factory delegate) 我想根据用于实例化HWcontroller的参数创建一个具有行为的特定模拟对象。 我甚至想做什么? class SystemUnderTest { SystemUnderTest(Ia a, Ib b, Ic c, /** 15 other things **/ Func<Func, HwType, IHwManager> HwManagerFactory, Func HwControllerFactory) { } } class HwManager() { public Func<HwType, Func, HwManager> Factory; public HwManager(HwType type, Func ControlFactory) { //Constructor } } 代码我是unit […]

当类在Service Project中时,Moq会在不可覆盖的成员上抛出无效设置

我正在尝试对这个类ServizioController进行unit testing: public class ServizioController : IServizioController { public virtual void PerformAction(Super.Core.Servizio servizio) { } public virtual bool Start() { throw new NotImplementedException(); } public virtual bool Stop() { throw new NotImplementedException(); } public virtual bool Continue() { throw new NotImplementedException(); } } 如果这个类是测试或库项目的一部分,那就没问题了。 但是当它在服务项目中时,Moq正在向我抛出这个错误: Invalid setup on a non-overridable member: x => x.Start() 我的测试看起来像这样: […]

Assert.AreSame的这种情况应该返回true吗?

我正在测试我创建的存储库模式,并使用Moq包来模拟我的对象。 我想测试来自2个对象的引用,但结果让我感到惊讶。 这是测试: Mock<Repository> moqRepo; Mock<Repository> moqRepo2; public void ObjEqGetTest() { //context is DBContext and has been initialized using [TestInitialize] annotation moqRepo = new Mock<Repository>(context); moqRepo2 = new Mock<Repository>(context); var test = moqRepo.Object.Get(1L); var test2 = moqRepo2.Object.Get(1L); Assert.AreSame(test, test2); } 我的Get方法返回: return entities.SingleOrDefault(predicate) 使用Expression构建器创建predicate (如果需要,我可以添加代码)。 当我创建了两个不同的对象时,为什么这个Assert返回true? 每当您从数据库中获取数据时,Get方法是否返回相同的引用(因为它指向正在使用的模型)? 谢谢你的帮助! 编辑 @CodeCaster说Mocking repos将在我提出的请求中返回null。 但是当我在表Web_Documents中validation值时,Assertions返回true。 让我certificate这一点: public void […]

Mock上的设置未返回预期值

这是我遇到的问题的简化版本: public interface IService { IProvider Provider { get; } } public interface IProvider { List Numbers{ get; } string Text { get; } } [TestMethod] public void ServiceTest() { var service = new Mock(); var provider = new Mock(); service.Setup(s => s.Provider).Returns(provider.Object); // A service.Setup(s => s.Provider.Text).Returns(“some text”); // B – incorrect // they […]

使用Shims for ZipFile进行unit testing

我正在尝试对使用ZipFile.OpenRead的一些代码进行unit testing,从ZIP中提取一些XML文件(用moq编写unit testing) 有没有办法用我自己的结果替换对ZipFile.OpenRead的调用? 我已经在类似的情况下使用垫片,但我无法弄清楚在这种情况下该做什么,并且关于垫片的文档非常稀疏。 这是(部分)需要unit testing的方法: public IEnumerable ExtractXmlFromZip(string fileName) { var configs = new List(); using (var archive = ZipFile.OpenRead(fileName)) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.FullName.EndsWith(“.xml”, StringComparison.OrdinalIgnoreCase)) { LoadConfigfromZipArchiveEntry(entry, configs) } } } return configs; }

使用Moq对LINQ to SQL CRUD操作进行unit testing

我已经浏览了其他问题,但没有什么能与我正在寻找的东西相提并论……主要是因为我并不是100%肯定我正在寻找什么! 基本上我目前正在开发一个新项目,我已经为数据库实体创建了抽象层,并将DAC设置为存储库。 我想用Mock对象对它进行unit testing,但是我已经用CRUD(特别是C)操作打了一个知识墙,我的unit testing类: [TestClass] public class RepositoryTest { private Mock _repository; private IList _personStore; [TestInitialize()] public void Initialize() { _personStore= new List() { new Person() { Name = “Paul” }, new Person() { Name = “John” }, new Person() { Name = “Bob” }, new Person() { Name = “Bill” }, }; _repository […]

如何模拟LINQ to Entities助手,例如’SqlFunctions.StringConvert()’

我正在使用EF 4,并尝试使用Moq对以下行进行unit testing: var convertError = models .Where(x => SqlFunctions.StringConvert((decimal?) (x.convert ?? 0)) == “0”) .Any(); 并且看起来SqlFunctions.StringConvert()会在检测到上下文被SqlFunctions.StringConvert()时抛出。 它给出了一个错误说: 只能从LINQ到实体调用此函数 是否有可能告诉SqlFunctions.StringConvert返回一个模拟对象,以便我可以摆脱这个错误?

如何让AutoFixture AutoMoq从实例化对象中的注入服务返回结果?

我正在尝试测试消费者存储库服务的服务类。 我设置了自定义设置,我相信它应该与我的存储库服务一起使用,而是返回默认的匿名结果。 如果您查看下面的代码示例,我正在尝试获取我在定制类中注册的“Foo”对象,当我调用svc.GetFoos方法时,我什么也得不到: void Main() { var fixture = new Fixture().Customize( new CompositeCustomization( new Customization(), new AutoMoqCustomization())); var svc = fixture.CreateAnonymous(); Console.Write(svc.GetFoos().Count()); } // Define other methods and classes here public class Bar { public IQueryable GetFoos() { return _rep.Query(); } public Bar(IRepository rep) { _rep = rep; } private IRepository _rep; } public class […]