moq

Unit Testing the Repository Pattern Without EF [closed]

那年仲夏 提交于 2021-01-20 13:40:07
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 18 hours ago . Improve this question I have created a simple Web API that interfaces with a MySQL Database. I am old school and like using SqlConnection/SqlCommand classes to interact with the database, no ORM/no EF. I have created a MySQL DbContext so that I can use DI to inject the MySQL specifics and

C# Mocked (Moq) Method not being called

可紊 提交于 2021-01-05 07:25:44
问题 I am trying to test an API client, but for some reason, I do not see one of the my internal methods being called. Specially, it seems that the AdminClient.MakeHttpCall is never called (based on my breakpoint set there there). Unsure what I am missing here, and have used Moq before. **** EDIT **** To clarify, in GetUserAsync method, line var response = await MakeHttpCall(HttpMethod.Get, $"/api/customadmin/user/id/{id}"); , the variable response is null , and it doesn't seem like the debugger

C# Mocked (Moq) Method not being called

自古美人都是妖i 提交于 2021-01-05 07:23:08
问题 I am trying to test an API client, but for some reason, I do not see one of the my internal methods being called. Specially, it seems that the AdminClient.MakeHttpCall is never called (based on my breakpoint set there there). Unsure what I am missing here, and have used Moq before. **** EDIT **** To clarify, in GetUserAsync method, line var response = await MakeHttpCall(HttpMethod.Get, $"/api/customadmin/user/id/{id}"); , the variable response is null , and it doesn't seem like the debugger

Specific questions about unit-testing Service Fabric Applications using Mocks

六眼飞鱼酱① 提交于 2021-01-05 07:08:41
问题 This question is a follow up to: Writing UnitTests for a Service Fabric Application Class My application definition is of the type - simplified it to solve the most basic of problems I am facing: namespace SearchService { internal sealed class SearchServiceClass : StatelessService { //variables defined followed by constructor private string jsonStr; public SearchServiceClass(StatelessServiceContext context) : base(context) { try { var dataPackage = Context.CodePackageActivationContext

Specific questions about unit-testing Service Fabric Applications using Mocks

≯℡__Kan透↙ 提交于 2021-01-05 07:07:49
问题 This question is a follow up to: Writing UnitTests for a Service Fabric Application Class My application definition is of the type - simplified it to solve the most basic of problems I am facing: namespace SearchService { internal sealed class SearchServiceClass : StatelessService { //variables defined followed by constructor private string jsonStr; public SearchServiceClass(StatelessServiceContext context) : base(context) { try { var dataPackage = Context.CodePackageActivationContext

When unit testing, how do I mock a return null from async method?

非 Y 不嫁゛ 提交于 2020-12-29 08:59:30
问题 Normally, I mock my repo like so: var repository = new Mock<ISRepository>(); repository.Setup(r => r.GetMemberAsync(email)) .Returns(Task.FromResult(new Member { FirstName = firstName, LastName = lastName })); But, in my code, I check to see if the member is not found, i.e. GetMemberAsync returns null. How do I mock this? I tried: var repository = new Mock<ISRepository>(); repository.Setup(r => r.GetMemberAsync(email)) .Returns(Task.FromResult<object>(null)); but I get a compile error. 回答1:

MVVM、MVVMLight、MVVMLight Toolkit之我见

╄→гoц情女王★ 提交于 2020-12-19 01:59:30
原文: MVVM、MVVMLight、MVVMLight Toolkit之我见 我想,现在已经有不少朋友在项目中使用了MVVMLight了吧,如果你正在做WPF,Silverlight,Windows Phone的开发,那么,你有十分必要的理由了解MVVM和MVVMLight。我写这篇文章的目的,是给大家做一个总结,以便更多的朋友了解并掌握MVVM。 首先,要说一下MVVM的概念。 MVVM严格来说,并不是一种框架,而是一个设计的模式吧 。与它有关的设计模式还有MVC (现在广泛用于Web应用中),以及MVP(之前有用过在Windows Forms和WPF中) 如果你希望对MVVM有更加感性地认识,我推荐你看下面这篇文章。 http://www.codeproject.com/KB/WPF/WpfMvvmQuickStart.aspx 这篇文章写得实在太好了,我很欣赏这样的技术人才,能把一个抽象问题有层次地讲清楚。(我强烈建议对MVVM的概念了解不深的朋友,认真读这篇文章,而不要急于用MVVMLight,因为MVVM是一种模式,而MVVMLight只是其中一种具体的实现) 然后,我要说一下MVVMLight吧,刚才说了,它是一种MVVM的实现。自然它不是唯一的一种实现,但现在 大家公认的是,它是比较好的一个实现 。就我个人的体会来说,我以前用过微软提供的Prism中的MVVM特性

.NET core write better unit tests with Xunit + Autofixture + Moq

◇◆丶佛笑我妖孽 提交于 2020-12-13 07:07:40
问题 In .NET Core for unit testing, I'm using Xunit, Moq, and Autofixture. But even with them, I see that my unit tests become complicated and take time. Maybe someone could tell me if there are any ways to make this test smaller? [Fact] public async Task Verify_NotAuthorised_NoServiceSendInvoked() { // Arrange var fixture = new Fixture() .Customize(new AutoMoqCustomization()); var sut = fixture.Create<VerificationService>(); var mockApiBuilder = fixture.Freeze<Mock<IApiEntityBuilder>>(); //init

.NET core write better unit tests with Xunit + Autofixture + Moq

自闭症网瘾萝莉.ら 提交于 2020-12-13 07:02:46
问题 In .NET Core for unit testing, I'm using Xunit, Moq, and Autofixture. But even with them, I see that my unit tests become complicated and take time. Maybe someone could tell me if there are any ways to make this test smaller? [Fact] public async Task Verify_NotAuthorised_NoServiceSendInvoked() { // Arrange var fixture = new Fixture() .Customize(new AutoMoqCustomization()); var sut = fixture.Create<VerificationService>(); var mockApiBuilder = fixture.Freeze<Mock<IApiEntityBuilder>>(); //init

Mocking HttpClient GetAsync by using Moq library in Xunit test

ε祈祈猫儿з 提交于 2020-12-12 06:28:25
问题 I am writing a simple unit test for this small service that simply calls external APIs: public class ApiCaller : IApiCaller { private readonly IHttpClientFactory _httpFactory; public ApiCaller(IHttpClientFactory httpFactory) { _httpFactory = httpFactory; } public async Task<T> GetResponseAsync<T>(Uri url) { using (HttpClient client = _httpFactory.CreateClient()) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.Timeout = TimeSpan