nsubstitute

Manipulating objects with DbSet<T> and IQueryable<T> with NSubstitute returns error

会有一股神秘感。 提交于 2019-12-06 08:54:30
问题 I'd like to use NSubstitute to unit test Entity Framework 6.x by mocking DbSet. Fortunately, Scott Xu provides a good unit testing library, EntityFramework.Testing.Moq using Moq. So, I modified his code to be suitable for NSubstitute and it's been looking good so far, until I wanted to test DbSet<T>.Add() , DbSet<T>.Remove() methods. Here's my code bits: public static class NSubstituteDbSetExtensions { public static DbSet<TEntity> SetupData<TEntity>(this DbSet<TEntity> dbset, ICollection

Two dimensional object array return type - NSubstitute

北慕城南 提交于 2019-12-05 18:03:41
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's a NSubstitute bug? NSubstitute depends on Castle, which depends on Reflection.Emit, so they blame

Mocking out expression with NSubstitute

谁都会走 提交于 2019-12-05 16:46:42
I have a interface that contains the following method signature: TResult GetValue<T, TResult>(object key, Expression<Func<T, TResult>> property) where T : class; Using Moq, I'm able to mock a specific call of this method like this: var repo = new Mock<IRepository>(); repo.Setup(r => r.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId)).Returns("SecretAgentId"); Then when I do this call repo.Object.GetValue<Customer, string>("SomeCustomerId", c => c.SecretAgentId); Tt returns "SecretAgentId" as I expect, so everything looks fine. My problem is that in our real production code we

How to mock 'out' parameter?

痴心易碎 提交于 2019-12-05 12:15:43
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 IDirectoryInfoWrap DirectoryInfo); } ...and the test: public void SetNetworkPath_SetDirectoryInfo() { var

NSubstitute - Received for async - “call is not awaited”warning

谁说胖子不能爱 提交于 2019-12-05 09:27:18
问题 I am trying to verify that an asynchronous method was called with the correct parameters. However, I get the warning: "Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call". This warning appears on the line of code underneath the //Assert comment (below). My test using NSubstitute is as follows: [Test] public async Task SimpleTests() { //Arrange var request =

Does NSubstitute support the idea of Partial Mocks?

旧巷老猫 提交于 2019-12-05 04:15:10
Does NSubstitute support the idea of Partial Mocks? http://nsubstitute.github.com/ http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx Yes! This feature was introduced in version 1.7.0 (released January 2014). http://nsubstitute.github.io/help/partial-subs/ 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 group . I've added an issue for this to the issue log , so you could also add details there. I am using version 1

Manipulating objects with DbSet<T> and IQueryable<T> with NSubstitute returns error

隐身守侯 提交于 2019-12-04 14:09:50
I'd like to use NSubstitute to unit test Entity Framework 6.x by mocking DbSet . Fortunately, Scott Xu provides a good unit testing library, EntityFramework.Testing.Moq using Moq . So, I modified his code to be suitable for NSubstitute and it's been looking good so far, until I wanted to test DbSet<T>.Add() , DbSet<T>.Remove() methods. Here's my code bits: public static class NSubstituteDbSetExtensions { public static DbSet<TEntity> SetupData<TEntity>(this DbSet<TEntity> dbset, ICollection<TEntity> data = null, Func<object[], TEntity> find = null) where TEntity : class { data = data ?? new

NSubstitute - TestFixture 1 causes AmbiguousArgumentsException in TestFixture 2

£可爱£侵袭症+ 提交于 2019-12-04 11:11:56
问题 I am writing C# unit tests using NUnit and NSubstitute. I am testing a class which will attempt to retrieve objects from a config provider implementing the following interface: public interface IConfigProvider<T> { T GetConfig(int id); T GetConfig(string id); } The class being tested only uses the int version of GetConfig so in the SetUpFixture I do the following to set up a mocked config provider that will always return the same dummy object: IConfigProvider<ConfigType> configProvider =

NSubstitute - Received for async - “call is not awaited”warning

泪湿孤枕 提交于 2019-12-03 22:16:20
I am trying to verify that an asynchronous method was called with the correct parameters. However, I get the warning: "Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call". This warning appears on the line of code underneath the //Assert comment (below). My test using NSubstitute is as follows: [Test] public async Task SimpleTests() { //Arrange var request = CreateUpdateItemRequest(); databaseHelperSub.ExecuteProcAsync(Arg.Any<DatabaseParams>()).Returns(Task.FromResult((object

How can I create and populate my mock classes with Autofixture?

雨燕双飞 提交于 2019-12-03 05:15:55
Currently I'm using EF6 to implement my repositories inside a UnitOfWork. I also have created an In-Memory mock implementations (MockUnitOfWork & MockRepository) so that I can use them in unit tests, however I now have to deal with the tedious setup of the objects. Isn't this what Autofixture is designed to do? How would I go about getting a MockUnitOfWork that I can use in my tests that contains Foo and Barr repositories that are populated? I'm using NSubstitute for my mocking framework. IUnitOfWork public interface IUnitOfWork { void Save(); void Commit(); void Rollback(); IRepository<Foo>