Tag: nsubstitute

NSubstitute模拟扩展方法

我想做模拟扩展方法,但它不起作用。 如何才能做到这一点? public static class RandomExtensions { public static IEnumerable NextInt32s(this System.Random random, int neededValuesNumber, int minInclusive, int maxExclusive) { // … } } [Fact] public void Select() { var randomizer = Substitute.For(); randomizer.NextInt32s(3, 1, 10).Returns(new int[] { 1, 2, 3 }); }

NSubstitute – TestFixture 1在TestFixture 2中导致AmbiguousArgumentsException

我正在使用NUnit和NSubstitute编写C#unit testing。 我正在测试一个类,它将尝试从实现以下接口的配置提供程序中检索对象: public interface IConfigProvider { T GetConfig(int id); T GetConfig(string id); } 正在测试的类只使用了GetConfig的int版本,因此在SetUpFixture中我执行以下操作来设置一个总是返回相同虚拟对象的模拟配置提供程序: IConfigProvider configProvider = Substitute.For<IConfigProvider>(); configProvider.GetConfig(Arg.Any()).Returns(new ConfigType(/* args */); 如果TestFixture是唯一运行的TestFixture,则运行绝对正常。 但是,在同一个程序集中的不同TestFixture中,我检查接收到的调用如下: connection.Received(1).SetCallbacks(Arg.Any<Action>(), Arg.Any<Action>(), Arg.Any<Action>()); 如果这些Received测试在配置提供程序测试之前运行,则配置测试在SetUpFixture中失败并出现AmbiguousArgumentsException: Here.Be.Namespace.ProfileManagerTests+Setup (TestFixtureSetUp): SetUp : NSubstitute.Exceptions.AmbiguousArgumentsException : Cannot determine argument specifications to use. Please use specifications for all arguments of the same type. at NSubstitute.Core.Arguments.NonParamsArgumentSpecificationFactory.Create(Object argument, IParameterInfo […]

如何使用Autofixture创建和填充我的模拟类?

目前我正在使用EF6在UnitOfWork中实现我的存储库。 我还创建了一个In-Memory模拟实现(MockUnitOfWork和MockRepository),以便我可以在unit testing中使用它们,但是我现在必须处理对象的繁琐设置。 这不是Autofixture的设计目的吗? 我如何获得可以在包含Foo和Barr存储库的测试中使用的MockUnitOfWork? 我正在使用NSubstitute作为我的模拟框架。 IUnitOfWork public interface IUnitOfWork { void Save(); void Commit(); void Rollback(); IRepository FooRepository { get; } IRepository BarRepository { get; } } IRepository public interface IRepository where TEntity : class { Func<IQueryable, IOrderedQueryable> orderBy = null, string includeProperties = “”); IEnumerable Get(Expression<Func> filter = null, Func<IQueryable, IOrderedQueryable> orderBy = […]

如何使用AutofacContrib.NSubstitute监视测试中的类

我正在使用NSpec框架,AutofacContrib.NSubstitute v3.3.2.0,NSubstitute v1.7.0.0(最新版本为1.8.2)的类库项目中运行unit testing。 Class Under Test实例是使用AutoSubstitute ,以便自动锁定所有需要的依赖项。 AutoSubstitute autoSubstitute = new AutoSubstitute(); MainPanelViewModel viewModel = autoSubstitute.Resolve(); 如果工作正常,我的Class Under Test会在某个时候调用其中一个基类方法,并带有一些特定的输入参数(基类不在我的控制范围内): // … base.ActivateItem(nextScreen); // … 因此,对于测试期望,我需要检查(间谍)实例调用基本方法: viewModel.Received().ActivateItem(Arg.Any()); 这是问题:当我尝试这样做时,在运行时NSubstitute抱怨我只能对使用Substitute.For()创建的对象运行Received() Substitute.For() 。 我还快速检查了AutofacContrib.NSubstitute源代码,但我找不到一种方法来获取具有自动锁定的实例,同时将它以某种方式包装在间谍对象或类似的东西中。 我也认为也许Substitute.ForPartsOf()可能会有所帮助,但在NSubstitute v1.7.0中似乎找不到该方法。 为了完整起见,这里的NSubstitute完全错误: 像.Received()这样的NSubstitute扩展方法只能在使用Substitute.For()和相关方法创建的对象上调用。

模拟对象没有Intellisense中显示的所有属性 – 在一个项目中但在另一个项目中有它们

我在嘲笑VSTO对象,在一个项目中(我没写),它有这样的代码: var listOfSheets = new List(); var mockSheets = Substitute.For(); mockSheets.Count.Returns(listOfSheets.Count); mockSheets的Intellisense ToolTip显示了6个属性: 具有断点的线在此项目中起作用。 但是我在不同的项目中使用相同的代码(相同的引用,名称空间等),但是mockSheets的Intellisense ToolTip只显示1个属性: 我知道这是我试图解决的根本原因,但实际问题是: 无法对空引用执行运行时绑定 编辑: Sheet对象被模拟: public static Worksheet Sheet { get { var mockSheet = Substitute.For(); mockSheet.Name = MockSheetName; mockSheet.Visible = XlSheetVisibility.xlSheetVisible; return mockSheet; } } public static Workbook Workbook() { return Workbook(1); }

返回返回另一个替换的方法的结果会在NSubstitute中引发exception

我在使用NSubstitute几次时遇到了一个奇怪的问题,虽然我知道如何解决它,但我从来没有能够解释它。 我已经精心设计了certificate问题的最低要求测试,它似乎与使用方法创建替代返回值有关。 public interface IMyObject { int Value { get; } } public interface IMyInterface { IMyObject MyProperty { get; } } [TestMethod] public void NSubstitute_ReturnsFromMethod_Test() { var sub = Substitute.For(); sub.MyProperty.Returns(MyMethod()); } private IMyObject MyMethod() { var ob = Substitute.For(); ob.Value.Returns(1); return ob; } 当我运行上面的测试时,我得到以下exception: Test method globalroam.Model.NEM.Test.ViewModel.DelayedAction_Test.NSubstitute_ReturnsFromMethod_Test threw exception: NSubstitute.Exceptions.CouldNotSetReturnException: Could not find […]

NSubstitute vs PRISM EventAggregator:断言调用方法会触发具有正确有效负载的事件

考虑以下方法,通过PRISM EventAggregator更新人员并发布事件,以指示此人已更新。 我想unit testing消息是否与正确的有效负载一起发送。 在这种情况下,这意味着正确的personId。 public void UpdatePerson(int personId) { // Do whatever it takes to update the person // … // Publish a message indicating that the person has been updated _eventAggregator .GetEvent() .Publish(new PersonUpdatedEventArgs { Info = new PersonInfo { Id = personId, UpdatedAt = DateTime.Now }; }); } 我知道我可以创建事件聚合器的替代品: var _eventAggregator = […]

NSubstitute – 测试特定的linq表达式

我正在使用我正在开发的MVC 3应用程序中的存储库模式。 我的存储库界面如下所示: public interface IRepository where TEntity : IdEntity { void Add(TEntity entity); void Update(TEntity entity); void Remove(TEntity entity); TEntity GetById(int id); IList GetAll(); TEntity FindFirst(Expression<Func> criteria); IList Find(Expression<Func> criteria); } 在很多实例中,当我在服务类中编码方法时,我使用的是FindFirst和Find方法。 如您所见,它们都将linq表达式作为输入。 我想知道的是,NSubstitute是否允许您在代码中指定要测试的特定表达式。 所以,这是一个服务方法的例子,它说明了我提到的一个Repository方法的使用: public IList GetUnprocessedInvoices() { try { var invoices = _invoiceRepository.Find(i => !i.IsProcessed && i.IsConfirmed); var dtoInvoices = Mapper.Map<IList, IList>(invoices); […]