nsubstitute

Mocked method returning null when called from a different project

≡放荡痞女 提交于 2019-12-24 21:54:25
问题 I have this test class: using NSubstitute; using NUnit.Framework; using System; using System.Linq.Expressions; namespace MyTests { public class Tests { [Test] public void Test() { var companyBL = Substitute.For<ICompanyBL>(); companyBL.GetCompany(c => new { c.RegionID }).ReturnsForAnyArgs(new { RegionID = 4, }); var company = companyBL.GetCompany(c => new { c.RegionID }); var dataRetriever = new DataRetriever(companyBL); } } } and this code in another project: namespace MyTests { using System

How to raise an event with NSubstitute with EventHandler<T>?

浪子不回头ぞ 提交于 2019-12-24 11:15:58
问题 I have an interface which defines multiple events, some with delegate type EventHandler<T> with T for example <string> . Objects implementing this interface are used in another class, a simple arrangement would look like this: public interface IEventEmitter { event EventHandler mySimpleEvent; event EventHandler<string> myStringEvent; } public class EventObserver { public IEventEmitter Emitter; public int called = 0; public EventObserver(IEventEmitter emitter) { Emitter = emitter; Emitter

Cannot determine argument specifications to use

人盡茶涼 提交于 2019-12-24 01:19:06
问题 I'm having a problem with NSubstitute. I have this short code: ReportingCycleDeliveryRepository .When(f => f.Add(Arg.Any<ReportingCycleDelivery>())) .Do(x => RepCycleDeliveries.Add((ReportingCycleDelivery)x[0])); So when my (void) method ReportingCycleDeliveryRepository.Add() is invoked with any ReportingCycleDelivery argument, it should add this item to my RepCycleDeliveries list. But instead, it throws an exception: NSubstitute.Exceptions.AmbiguousArgumentsException "Cannot determine

Function is called when I do not expect it to with NSubstitute

◇◆丶佛笑我妖孽 提交于 2019-12-23 21:25:57
问题 I am getting a behaviour I didnt expect from NSubstitute when setting up my mocks to call a function. A simplified version of the behavuiour is [Test] public void NSubstituteTest() { var mockedFoo = Substitute.For<IFoo>(); mockedFoo.GenerateString(Arg.Any<string>()).Returns(x => GetValue(x.Args()[0])); mockedFoo.GenerateString("0").Returns("hi"); string result1 = mockedFoo.GenerateString("0"); string result2 = mockedFoo.GenerateString("1"); Assert.AreEqual("hi", result1); Assert.AreEqual("1",

NSubstitute: Mock method is not returning expected result

二次信任 提交于 2019-12-23 21:18:12
问题 I'm new to NSubstitute, mocking, and unit testing in general. I'm trying to use NSubstitute to remove some dependencies I have in my class under test, but methods in the mock objects are not behaving as I expect based on how I configured them. Here is an example I created in Visual Studio: Interface and concrete class to be substituted. Notice, MyConcreteClass.MyMethod() returns false : public interface IMyInterface { bool MyMethod(string arg); } public class MyConcreteClass : IMyInterface {

How to substitute Object.ToString using NSubstitute?

此生再无相见时 提交于 2019-12-23 07:39:44
问题 When I try to use NSubstitute 1.7.1.0 to define behaviour of Object.ToString (which is a virtual method), NSubstitute is throwing an exception. To reproduce: [Test] public static void ToString_CanBeSubstituted() { var o = Substitute.For<object>(); o.ToString().Returns("Hello world"); Assert.AreEqual("Hello world", o.ToString()); } The failure: NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException : Could not find a call to return from. Make sure you called Returns() after calling

Check calls Received() for async method

旧时模样 提交于 2019-12-23 07:19:07
问题 When I run the following code: [Test] public async Task Can_Test_Update() { var response = await _controller.UpdateAsync(Guid.NewGuid()); response.Valid.Should().BeTrue(); _commands.Received().UpdateAsync( Arg.Is<Something>( l => l.Status == Status.Updated)); } If I add " await " preceding the " _commands.Received().UpdateAsync ", it throws a null reference exception. How can I stop this happening, or is await not necessary? 回答1: I found an answer here. Received.InOrder(async () => { await

How to mock 'out' parameter?

删除回忆录丶 提交于 2019-12-22 09:00:57
问题 I have downloaded the latest NSubstitute release, 1.1.0, May 21, 2011. Prior to this release, it seems that NSub did not support out parameters. It appears that some work has been done to provide support through an intermediate release: NSub Google Group. So, I am having a little trouble trying to get all the pieces working. I am using SystemWrapper to mock DirectoryInfo Here is my interface: public interface INetworkPath { void SetPath(string NetworkPath); bool TryGetDirectoryInfo(out

Two dimensional object array return type - NSubstitute

痴心易碎 提交于 2019-12-22 08:37:12
问题 I get a cast exception System.InvalidCastException : Unable to cast object of type 'System.Object[]' to type 'System.Object[,]'. at Castle.Proxies.ITestProxy.Get2DArray() at Scratch.TestFixture.Get2DArray() in TestTest.cs: line 17 from from the below: [TestFixture] public class TestFixture { [Test] public void Get2DArray() { Substitute.For<ITest>().Get2DArray().Returns(new object[1,1]); } } public interface ITest { object[,] Get2DArray(); } can anyone throw any light on this? I'm thinking it

NSubstitute mock extension method

点点圈 提交于 2019-12-21 04:24:52
问题 I want to do mock extension method, but it does not work. How can this be done? public static class RandomExtensions { public static IEnumerable<int> NextInt32s(this System.Random random, int neededValuesNumber, int minInclusive, int maxExclusive) { // ... } } [Fact] public void Select() { var randomizer = Substitute.For<DefaultRandom>(); randomizer.NextInt32s(3, 1, 10).Returns(new int[] { 1, 2, 3 }); } 回答1: NSubstitute can not mock extension methods as per Sriram's comment, but you can still