nsubstitute

Internal properties are not handled by NSubstitute

喜欢而已 提交于 2020-06-26 06:59:19
问题 Suppose, that I've got a following class: public abstract class Test { internal abstract int Prop { get; } } Now, I try to make a mock using NSubstitute: var mock = Substitute.For<Test>(); But that fails: Method 'get_Prop' in type 'Castle.Proxies.TestProxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' does not have an implementation. I thought of adding NSubstitute to [InternalsVisibleTo] , but unfortunately my tested assembly is

How do I mock DbContext using NSubstitute and then add/remove data

↘锁芯ラ 提交于 2020-05-12 15:42:20
问题 I need to mock EF's DbContext . I use the approach here and it works well. // mock a DbSet var mockDbSet = Substitute.For<DbSet<Foo>, IQueryable<Foo>>(); var data = new List<Foo>().AsQueryable(); ((IQueryable<Foo>)mockDbSet).Provider.Returns(data.Provider); ((IQueryable<Foo>)mockDbSet).Expression.Returns(data.Expression); ((IQueryable<Foo>)mockDbSet).ElementType.Returns(data.ElementType); ((IQueryable<Foo>)mockDbSet).GetEnumerator().Returns(data.GetEnumerator()); // now add it to a mock

Mocked method returns null when using anonymous types

风流意气都作罢 提交于 2020-01-25 02:57:25
问题 I have this code: using NSubstitute; using NUnit.Framework; using System; using System.Linq.Expressions; namespace MyTests { public interface ICompanyBL { T GetCompany<T>(Expression<Func<Company, T>> selector); } public partial class Company { public int RegionID { get; set; } } public class Tests { [Test] public void Test() { var companyBL = Substitute.For<ICompanyBL>(); //Doesn't work companyBL.GetCompany(c => new { c.RegionID }).Returns(new { RegionID = 4, }); //Results in null: var

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

与世无争的帅哥 提交于 2020-01-22 11:03:49
问题 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.

How to set value to a local variable of a class using NSubstitute in TestProject?

坚强是说给别人听的谎言 提交于 2020-01-14 05:52:27
问题 I need to mock with NSubstitute and need to set local variable command of a class LoanCreateHandler to mock data with it's parameter Z . I have code like give below: public class ClassA { public string Prop1 { get; set; } public string Prop2 { get; set; } … // Here I have some other properties } public class CreateLoanCommand { public string X { get; set; } public string Y { get; set; } public ClassA Z { get; set; } } public class LoanCreateHandler { public Response Handle(LoanCreateRequest

NSubstitute: Mocking the request, response object inside a MVC/Web Api Controller?

走远了吗. 提交于 2020-01-03 17:10:54
问题 I am trying to find how to mock both the Request and Response objects that are available inside a controller from MVC / Web Api. Is this possible, I am not injecting the Request and Response objects, these are available because the controllers inherit from ApiController or Controller. Does anyone have some good examples for getting access to these through nsubstitute ? Also what about the other objects like context ? 回答1: You do not have to mock them. Since they have read/write properties,

Unable to cast object of type 'Castle.Proxies.XProxy' to type 'X' in WPF Designer

ⅰ亾dé卋堺 提交于 2020-01-03 09:16:12
问题 I've recently discovered the very useful design time attributes from Blend for a WPF component, which (among other things) allows you to set a DataContext only at design time. Awesome! Combined with the DesignInstance attribute, you can set a type to be automatically created and bound to during design time, allowing you to use the Visual Studio Designer with some context as to what your WPF component will actually look like at run time. Its really nice, and I wish it hadn't taken me so long

Mocking out expression with NSubstitute

こ雲淡風輕ζ 提交于 2020-01-02 06:11:44
问题 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

How to spy the class under test with AutofacContrib.NSubstitute

让人想犯罪 __ 提交于 2019-12-31 04:39:07
问题 I'm running unit tests in a class library project with NSpec framework, AutofacContrib.NSubstitute v3.3.2.0, NSubstitute v1.7.0.0 (the latest as of now is 1.8.2). The Class Under Test instance is built with AutoSubstitute , in order to automock all its needed dependencies. AutoSubstitute autoSubstitute = new AutoSubstitute(); MainPanelViewModel viewModel = autoSubstitute.Resolve<MainPanelViewModel>(); If working properly, my Class Under Test at some point will invoke one of it's base class

Mocked object doesn't have all properties shown in Intellisense - in one project but has them in the other

夙愿已清 提交于 2019-12-30 11:49:13
问题 I'm mocking VSTO objects and in one project (I didn't write) it has this code: var listOfSheets = new List<Worksheet>(); var mockSheets = Substitute.For<Sheets>(); mockSheets.Count.Returns(listOfSheets.Count); The Intellisense ToolTip for the mockSheets shows 6 properties: The line with the break point works in this project. However I have the same code in a different project (same references, namespaces, etc.), yet the Intellisense ToolTip for the mockSheets only shows 1 property: I know