nsubstitute

Example of how to use AutoFixture with NSubstitute

耗尽温柔 提交于 2019-12-21 03:28:19
问题 I use NSubstitute a lot. And I love it. I am just looking into AutoFixture. It seems great! I have seen AutoFixture for NSubstitute and seen a few examples in Moq on how to use this feature. But I can't seem to translate it into NSubstitute. I tried this: var fixture = new Fixture().Customize(new AutoNSubstituteCustomization()); var addDest = Substitute.For<IPerson>(); Using: public interface IPersonEntity { int ID { get; set; } string FirstName { get; set;} string LastName { get; set;}

NSubstitute: Checking received methods with array arguments

五迷三道 提交于 2019-12-18 18:33:34
问题 I want to verify that a method on my NSubstitute mock is called with a particular array argument. Say the interface, IProcessor , has a method void ProcessSomething(Foo[] something]) . Say my class under test is named Commander . I set up my test like this: //prepare var processor = Substitute.For<IProcessor>; var commander = new Commander(processor); var foo1 = new Foo("alpha"); var foo2 = new Foo("bravo"); var foos = new [] {foo1, foo2}; //act commander.DoSomething(foo1, foo2); //verify

Using NSubstitute and Ninject to return a value

て烟熏妆下的殇ゞ 提交于 2019-12-14 02:23:06
问题 In my NinjectDependencyResolver: IDependencyResolver I have a AddBindings() method that for now I want it to return some hard coded values for me until I connect it to DB later. The class and interface I want to mock and use in that AddBindings() method are like this: public class Configuration { public string WebSiteNotActiveMessage { get; set; } public bool IsWebSiteActive { get; set; } } public interface IConfigurationManager { Models.Configuration.Configuration ConfigFileValues { get; } }

returning different values in an NSubstitute mock method with an out parameter

对着背影说爱祢 提交于 2019-12-14 02:05:20
问题 Given a method with which to mock... public bool TryReceive(out T message, TimeSpan millisecondsToWait) I wish to set different messages on the first two calls, and return true. Subsequent calls return false. I have tried a few variations, and in either case, the lambda expression is executed once , and never again. NSubstitute seems to be caching the first return value , and using the same value over and over. I have tried this... TCR @out; var container = new AutoSubstitute(); var mb =

Convert C# statements including NSubstitute code to VB.net

人盡茶涼 提交于 2019-12-13 16:15:50
问题 While reading the NSubstitute tutorial i convert the samples written in C# to VB.net to understand the functionality, but I need your support for these (unrelated) statements, which I can't convert despite all the care taken: 1. calculator().Received().Add(1, Arg.Is(Of Integer)(function(x) new[] {-2,-5,-10}.Contains(x))) 2. Note: foo is a derived object from an interface with a void method called "SayHello" foo.When(x >= x.SayHello("World")).Do(x => counter++); 3. calculator().When(x >= x.Add

Unit Tests: How to Assert? Asserting results returned or that a method was called on a mock?

对着背影说爱祢 提交于 2019-12-13 04:35:02
问题 I am trying to find out the best way to Assert, should I be creating an object with what i should return and check that it has equal to the expected result ? Or should I be running a method against a mock to ensure that the method was actually called. I have seen this done both ways, I wondered if anyone has any best practices for this. Of course, it's quicker and easier to write a unit test to assert that a method was called on the mock but quicker and easier is not always the best way -

Code analysis CA0001 error for project using NSubstitute

半腔热情 提交于 2019-12-13 03:38:25
问题 I have a (.NET 4) test project which references (the .NET 4) NSubstitute.dll. When I run CodeAnalysis against the project I get a number of CA0001 errors: Running Code Analysis... MSBUILD : error : CA0001 : The following error was encountered while reading module 'NSubstitute': Could not resolve type reference: [mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]System.Runtime.CompilerServices.ExtensionAttribute. MSBUILD : error : CA0001 : Rule=Microsoft

Is it OK to use MS Fakes shims with NSubstitute mocks?

送分小仙女□ 提交于 2019-12-12 21:15:13
问题 We are using NSubstitute to mock external objects for our unit tests. However many legacy classes are not called via interfaces and can't be easily replaced with mocks. I've considered to use Microsoft Fakes, but according to the answer for the question "Mock framework vs MS Fakes frameworks" " if you're already using a more full-featured mocking framework, you might feel like there are some important pieces missing from Fakes stubs." Would it be possible to use MS Fakes shims with

Arg.Do() is not firing when expected in a When..Do for void method

只愿长相守 提交于 2019-12-12 18:22:30
问题 I have the below structure in a test of mine, intended to test that a certain log is being called with the right complex argument object, even when it throws an exception which is then wrapped and generally manipulated further. The logThing has a method: void AddEntry(LogEntry); So I am using When..Do to make it throw an exception, public void UnitTest() { // Arrange ILogThing logThing = Substitute.For<ILogThing>() SystemUnderTest system = new SystemUnderTest(logThing); List<LogEntry>

Ninject: auto mocking using NSubstitute?

梦想与她 提交于 2019-12-12 14:14:27
问题 Can anyone help, I am having problems using the auto-mocking that is available between Ninject and NSubstitute, actually the package is a ninject packaged called Ninject.MockingKernel.NSubstitute which should allow me to use Ninject to create mocks and return instances with mocks injected. There seems to be a few examples for Moq and Rhinomocks but I don't see any for NSubstitute. What I have so far is this.kernel = new NSubstituteMockingKernel(); var summaryService = this.kernel.GetMock