nsubstitute

Mocking Action<T> with NSubstitute

我的梦境 提交于 2019-12-12 10:10:08
问题 A proxy for a webservice needs unit testing without - obviously - hitting the web service. This is the method I'd like to neuter - public void Invoke(Action<T> action) { Task.Run(async ()=> { await _invoker.Invoke(this, action); }).Wait(); } Is there any way to mock the Action parameter using NSubstitute? I've been working along the lines of using _proxy.When(x => x.Invoke($args_go_here)).Do(x => _counter++); but I'm having real troubles formulating the Args expressions. If I could simply

faking API calls /w NSubstitute, for unit testing

岁酱吖の 提交于 2019-12-11 20:01:24
问题 I got a lot of function calls like the one below that I want to unit test but are unsure of how i should approach functions like these.. Do I just test it with the real URL and API calls?? but then it won't be a real unit test since I including things which I don't have control of... which leads me to the conclusion that I have to mock the RestClient out?? where I need to make a RestClient Foo(ApiUrl + ApiDirectory); which I can use NSubtitute on, is it the right way?? Would you guys approach

How to test DB.Configuration.AutoDetectChangesEnabled = false

北城以北 提交于 2019-12-11 05:14:46
问题 I'm trying to write some tests for a class using NSubstitute. Class constructor is: public class ClassToTest : IClassToTest { private IDataBase DB; public ClassToTest(IDatabase DB) { this.DB = DB; this.DB.Configuration.AutoDetectChangesEnabled = false; } Here is my UnitTests class: [TestFixture] public class ClassToTestUnitTests { private ClassToTest _testClass; [SetUp] public void SetUp() { var Db = Substitute.For<IDatabase>(); //Db.Configuration.AutoDetectChangesEnabled = false; <- I've

NSubstitute When…Do definition is not getting overriden by subsequent definitions

女生的网名这么多〃 提交于 2019-12-11 04:09:29
问题 void ABC() { var foo = Substitute.For<IFoo>(); foo.When(x => x.Bar()).Do(x => counter++); <use Bar()>.... 1 foo.When(x => x.Bar()).Do(x => counter--); <use Bar()>.... 2 } For the above code snippet both (1) and (2) are displaying the counter++ behavior indicating that the When...Do behavior is not getting overridden. I need this behavior for generating my testing scenario where I want to hookup different callbacks. How should I achieve this? 回答1: The Do callback does not get replaced, but

NSubstitute not matching Linq Expression

拥有回忆 提交于 2019-12-11 03:57:46
问题 I am implementing a repository pattern Query class and testing using NSubstitute. Repository interface: public interface IMyRepository { IQueryable<T> Query<T>(Expression<Func<T, bool>> filter) where T : class; } DateTimeProvider interface: public interface IMyDateTimeProvider { DateTime GetDateNow(); } Application interface: public interface IMyApplication { List<Thing> GetThingsByQuery(int status); } Application implementation: public class MyApplication : IMyApplication { private readonly

NSubstitute and Unity

跟風遠走 提交于 2019-12-11 02:35:37
问题 I am currently trying to learn DI & Mocking, with Unity and NSubstitute. I am also using an automocking extension taken from this question: Is this possible with Unity (Instead of Castle Windsor)? In my unit test below I am trying to set an NSubstitute return value of 10 from my method Add(). However when debugging through the controller call the assigned value is the default 0 rather than the expected 10. The proxy does not seem to be intercepting the method call. I suspect this is caused by

How do I avoid using dynamic when mocking an Excel.worksheet?

有些话、适合烂在心里 提交于 2019-12-10 02:17:23
问题 I'm trying to mock an Excel spreadsheet using NSubstitute or other mocking framework and MSTest (Visual Studio 2010). I'm not sure if there's a better way than this--and this doesn't quite work for testing: Here's an example (this is all prototype code right now, and not very clean): int[] lowerBounds = { 1, 1 }; int[] lengths = { 2, 2 }; //Initialize a 1-based array like Excel does: object[,] values = (object[,])Array.CreateInstance(typeof(object), lengths, lowerBounds); values[1,1] = "hello

NSubstitute - mock out parameter behaviour for any parameter

喜你入骨 提交于 2019-12-08 19:58:51
问题 I'm trying to mock IConfigurationProvider with NSubstitute. I need the method bool TryGet(string key, out string value) to return values for differing keys. So something like this: var configProvider = Substitute.For<IConfigurationProvider>(); configProvider.TryGet("key1", out Arg.Any<string>()).Returns(x => { x[1] = "42"; return true; }); but this does not compile. I need the mocked method to actually set the out parameter to the appropriate value, regardless of what that parameter is - it's

NSubstitute Checking received calls don't work

点点圈 提交于 2019-12-08 01:06:36
问题 Hey guys im new with the NSubstitute framework. I'm trying to test some of my classes, but when i use NSubstitute to check received calls it says received no matching calls. I'm trying to test if the method Tick() is receiving update() from track class. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ATM_System.Event; using ATM_System.EventDetection; using ATM_System.Region; using ATM_System.Track; namespace ATM_System

Does NSubstitute support the idea of Partial Mocks?

我们两清 提交于 2019-12-07 00:08:52
问题 Does NSubstitute support the idea of Partial Mocks? http://nsubstitute.github.com/ http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx 回答1: Yes! This feature was introduced in version 1.7.0 (released January 2014). http://nsubstitute.github.io/help/partial-subs/ 回答2: Update: As @Brian points out, NSubstitute 1.7+ supports partial mocks Original answer: Not as of v1.0. It is something we're considering for vNext. If you have specific syntax ideas or requirements please post to the user