moq

Mocking EF core dbcontext and dbset

好久不见. 提交于 2020-06-09 12:02:48
问题 I am using ASP.NET Core 2.2, EF Core and MOQ. When I run the test I am getting this error: Message: System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x.Movies What I am doing wrong? public class MovieRepositoryTest { private readonly MovieRepository _sut; public MovieRepositoryTest() { var moviesMock = CreateDbSetMock(GetFakeListOfMovies()); var mockDbContext = new Mock<MovieDbContext>(); mockDbContext.Setup(x => x.Movies).Returns(moviesMock.Object

Can I use moq Mock<MyClass> to mock a class , not an interface?

人走茶凉 提交于 2020-05-28 20:15:36
问题 Going thru https://github.com/Moq/moq4/wiki/Quickstart, I see it Mock an interface. I have a class in my legacy code which does not have an interface, when I `Mock' , I get the following exception: Additional information: Can not instantiate proxy of class: MyCompnay.Mylegacy.MyClass. How can I use Moq to mock class from Legacy code? Thank you. 回答1: It is possible to Mock concrete classes [TestClass] public class PlaceholderParserFixture { public class Foo { public virtual int GetValue() {

Can I use moq Mock<MyClass> to mock a class , not an interface?

天涯浪子 提交于 2020-05-28 20:06:45
问题 Going thru https://github.com/Moq/moq4/wiki/Quickstart, I see it Mock an interface. I have a class in my legacy code which does not have an interface, when I `Mock' , I get the following exception: Additional information: Can not instantiate proxy of class: MyCompnay.Mylegacy.MyClass. How can I use Moq to mock class from Legacy code? Thank you. 回答1: It is possible to Mock concrete classes [TestClass] public class PlaceholderParserFixture { public class Foo { public virtual int GetValue() {

Moq IServiceProvider / IServiceScope

别来无恙 提交于 2020-05-10 03:44:59
问题 I am trying to create a Mock (using Moq) for an IServiceProvider so that I can test my repository class: public class ApiResourceRepository : IApiResourceRepository { private readonly IServiceProvider _serviceProvider; public ApiResourceRepository(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; _dbSettings = dbSettings; } public async Task<ApiResource> Get(int id) { ApiResource result; using (var serviceScope = _serviceProvider. GetRequiredService<IServiceScopeFactory>

Moq IServiceProvider / IServiceScope

陌路散爱 提交于 2020-05-10 03:44:11
问题 I am trying to create a Mock (using Moq) for an IServiceProvider so that I can test my repository class: public class ApiResourceRepository : IApiResourceRepository { private readonly IServiceProvider _serviceProvider; public ApiResourceRepository(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; _dbSettings = dbSettings; } public async Task<ApiResource> Get(int id) { ApiResource result; using (var serviceScope = _serviceProvider. GetRequiredService<IServiceScopeFactory>

关于单元测试的思考--Asp.Net Core单元测试最佳实践

删除回忆录丶 提交于 2020-05-06 09:13:18
在我们码字过程中,单元测试是必不可少的。但在从业过程中,很多开发者却对单元测试望而却步。有些时候并不是不想写,而是常常会碰到下面这些问题,让开发者放下了码字的脚步: 这个类初始数据太麻烦,你看:new MyService(new User("test",1), new MyDAO(new Connection(......)),new ToManyPropsClass(......) .....) 。我:。。。 这个代码内部逻辑都是和Cookie有关,我单元测试不好整啊,还是得启动到浏览器里一个按钮一个按钮点。 这个代码内部读了配置文件,单元测试也不能给我整个配置文件啊? 这个代码主要是验证WebAPI入口得模型绑定,必须得调用一次啊? 这些问题确实存在,但它们阻止不了我们那颗要写单元测试的心。单元测试的优点很多,你或许可以不管。但至少能让你从那些需要在浏览器里点击10多下的操作里解脱出来。 本文从一个简单的逻辑测试出发,慢慢拉开测试的大幕,让你爱上测试。文章主要是传播一些单元测试的理念,其次才是介绍asp.net core中的单元测试。 本文使用的环境为asp.net core 2.1 webapi,代码可以直接下载: https://github.com/yubaolee/DotNetCoreUnitTestSamples 为了方便阅读,以一个最简单的逻辑为例: public

When should I use the .As method of Moq?

别说谁变了你拦得住时间么 提交于 2020-04-29 15:43:09
问题 When exactly do we need to use the .As method provided by Moq? From the Quickstart documentation: // implementing multiple interfaces in mock var foo = new Mock<IFoo>(); var disposableFoo = foo.As<IDisposable>(); // now the IFoo mock also implements IDisposable :) disposableFoo.Setup(df => df.Dispose()); But I just don't get why we would want to do that. Could you give me a practical example? 回答1: Okay, so an example. Let's say you have a transportation management software to manage movement

When should I use the .As method of Moq?

五迷三道 提交于 2020-04-29 15:42:21
问题 When exactly do we need to use the .As method provided by Moq? From the Quickstart documentation: // implementing multiple interfaces in mock var foo = new Mock<IFoo>(); var disposableFoo = foo.As<IDisposable>(); // now the IFoo mock also implements IDisposable :) disposableFoo.Setup(df => df.Dispose()); But I just don't get why we would want to do that. Could you give me a practical example? 回答1: Okay, so an example. Let's say you have a transportation management software to manage movement

How to setup Mock of IConfigurationRoot to return value [duplicate]

谁都会走 提交于 2020-04-29 10:23:27
问题 This question already has answers here : Expression references a method that does not belong to the mocked object (4 answers) Closed 2 years ago . I have used IConfigurationRoute to access a directory like this. if (type == "error") directory = _config.GetValue<string>("Directories:SomeDirectory"); _config is IConfigurationRoot injected in the constructor. I tried the following way to mock it. var mockConfigurationRoot = new Mock<IConfigurationRoot>(); mockConfigurationRoot.Setup(c => c

Mock IHttpContextAccessor in Unit Tests

℡╲_俬逩灬. 提交于 2020-04-29 08:51:20
问题 I have a method to get header value using IHttpContextAccessor public class HeaderConfiguration : IHeaderConfiguration { public HeaderConfiguration() { } public string GetTenantId(IHttpContextAccessor httpContextAccessor) { return httpContextAccessor.HttpContext.Request.Headers["Tenant-ID"].ToString(); } } I am testing GetBookByBookId method Let's say the method looks like this: public class Book { private readonly IHttpContextAccessor _httpContextAccessor; private IHeaderConfiguration