nsubstitute

Mocking Generic Method with NSubstitute

此生再无相见时 提交于 2019-12-01 06:35:12
I have an interface with a number of generic methods. These methods perform operations based on the type of data that is passed in. How do I mock this with NSubstitute? At the moment, I had to resort to using a concrete class instead of a mock since I cannot handle all possible types that the method will be called with. public interface IInstanceSource { bool CanCreate<T>(); T Create<T>(); void Register<T>(Func<T> creator); } public static IInstanceSource GetInstanceSource() { var _data = new Dictionary<Type, Func<object>>(); var a = Substitute.For<IInstanceSource>(); //code below fails since

Mocking Generic Method with NSubstitute

折月煮酒 提交于 2019-12-01 03:37:06
问题 I have an interface with a number of generic methods. These methods perform operations based on the type of data that is passed in. How do I mock this with NSubstitute? At the moment, I had to resort to using a concrete class instead of a mock since I cannot handle all possible types that the method will be called with. public interface IInstanceSource { bool CanCreate<T>(); T Create<T>(); void Register<T>(Func<T> creator); } public static IInstanceSource GetInstanceSource() { var _data = new

How to combine PropertyData and AutoNSubstituteData attributes in xunit/autofixture?

折月煮酒 提交于 2019-11-30 21:26:00
I am using the [AutoNSubstituteData] attribute, which was posted here: AutoFixture, xUnit.net, and Auto Mocking I would like to combine this with the [PropertyData("")] attribute from xunit extensions. This is my test: public static IEnumerable<string[]> InvalidInvariant { get { yield return new string[] { null }; yield return new [] { string.Empty }; yield return new [] { " " }; } } [Theory, AutoNSubstituteData, PropertyData("InvalidInvariant")] public void TestThatGuardsAreTriggeredWhenConnectionStringArgumentIsInvalid( IDeal deal, IDbConnection conn, IDb db, ISender sender, string

NSubstitute: Checking received methods with array arguments

拥有回忆 提交于 2019-11-30 17:10:57
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 processor.Received().ProcessSomething(foos); // FAILS The Received() call fails with: NSubstitute

NSubstitute vs PRISM EventAggregator: Assert that calling a method triggers event with correct payload

倾然丶 夕夏残阳落幕 提交于 2019-11-30 09:24:45
问题 Consider the below method that updates a person and publishes an event through the PRISM EventAggregator to indicate that the person has been updated. I would like to unit test that the message is sent with the correct payload. In this case that would mean the correct 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<PersonUpdatedEvent>() .Publish

How to combine PropertyData and AutoNSubstituteData attributes in xunit/autofixture?

淺唱寂寞╮ 提交于 2019-11-30 05:48:39
问题 I am using the [AutoNSubstituteData] attribute, which was posted here: AutoFixture, xUnit.net, and Auto Mocking I would like to combine this with the [PropertyData("")] attribute from xunit extensions. This is my test: public static IEnumerable<string[]> InvalidInvariant { get { yield return new string[] { null }; yield return new [] { string.Empty }; yield return new [] { " " }; } } [Theory, AutoNSubstituteData, PropertyData("InvalidInvariant")] public void

NSubstitute test works by itself, but throws Unexpected Matcher Argument in a suite

五迷三道 提交于 2019-11-29 11:49:29
I have a unit test where I use .Returns() to return some sample data: [TestMethod] public void TestRetrieveElementsInVersion() { IRetrieveElementSequence component = Substitute.For<IRetrieveElementSequence>(); List<UnconstructedElement> list = new List<UnconstructedElement> { new UnconstructedElement{Version = "1"}, new UnconstructedElement{Version = "2"} }; component.RetrieveElements().Returns(list); // exception reported here const string target = "1"; IRetrieveElementSequence service = new RetrieveElementsInAVersion(component, target); IList<UnconstructedElement> result = service

Mock IDocumentQuery with ability to use query expressions

。_饼干妹妹 提交于 2019-11-28 05:25:40
问题 I need to be able to mock IDocumentQuery , to be able to test piece of code, that queries document collection and might use predicate to filter them: IQueryable<T> documentQuery = client .CreateDocumentQuery<T>(collectionUri, options); if (predicate != null) { documentQuery = documentQuery.Where(predicate); } var list = documentQuery.AsDocumentQuery(); var documents = new List<T>(); while (list.HasMoreResults) { documents.AddRange(await list.ExecuteNextAsync<T>()); } I've used answer from

NSubstitute test works by itself, but throws Unexpected Matcher Argument in a suite

ぃ、小莉子 提交于 2019-11-28 04:56:27
问题 I have a unit test where I use .Returns() to return some sample data: [TestMethod] public void TestRetrieveElementsInVersion() { IRetrieveElementSequence component = Substitute.For<IRetrieveElementSequence>(); List<UnconstructedElement> list = new List<UnconstructedElement> { new UnconstructedElement{Version = "1"}, new UnconstructedElement{Version = "2"} }; component.RetrieveElements().Returns(list); // exception reported here const string target = "1"; IRetrieveElementSequence service = new

NSubstitute - Testing for a specific linq expression

只谈情不闲聊 提交于 2019-11-27 15:11:11
I am using the repository pattern in an MVC 3 application I am currently developing. My repository interface looks as follows: public interface IRepository<TEntity> where TEntity : IdEntity { void Add(TEntity entity); void Update(TEntity entity); void Remove(TEntity entity); TEntity GetById(int id); IList<TEntity> GetAll(); TEntity FindFirst(Expression<Func<TEntity, bool>> criteria); IList<TEntity> Find(Expression<Func<TEntity, bool>> criteria); } In a lot of instances, when coding methods in my service classes, I am using the FindFirst and Find methods. As you can see, they both take a linq