moq

How to mock the new HttpClientFactory in .NET Core 2.1 using Moq

梦想的初衷 提交于 2021-01-29 10:00:44
问题 .NET Core 2.1 comes with this new factory called HttpClientFactory , but I can't figure out how to mock it to unit test some methods that include REST service calls. The factory is being injected using .NET Core IoC container, and what the method does is create a new client from the factory: var client = _httpClientFactory.CreateClient(); And then using the client to get data from a REST service: var result = await client.GetStringAsync(url); 回答1: The HttpClientFactory is derived from

Verify call to File.Delete with System.IO.Abstractions.TestingHelpers

醉酒当歌 提交于 2021-01-29 06:52:15
问题 I use the System.IO.Abstractions.TestingHelpers to mock FileSystem. In my class, I inject IFileSystem and use the instance to call _fileSystem.File.Exists and _fileSystem.File.Delete. In my test class, I would like to verify that the "Delete" method was called. It's easy by mocking only the IFile, but since I already mocked the FileSystem, I don't want to have to mock the Directory, Path and File on top of it. Is it possible to call something like _fileRepository.FileMock.Verify(x => x.Delete

Writing UnitTests for a Service Fabric Application Class

六月ゝ 毕业季﹏ 提交于 2021-01-29 06:05:09
问题 I am writing unit tests for a Service Fabric Application class. I am running into some errors which I don't understand how to fix. The class definition is of the sort: namespace SearchService { internal sealed class SearchServiceClass : StatelessService { //variables defined followed by constructor private string jsonStr; public SearchServiceClass(StatelessServiceContext context) : base(context) { //constructor stuff } public bool IsDataJsonLoaded { get { return !(jsonStr == null); } } } }

callback is not called using moq + autofaq

泪湿孤枕 提交于 2021-01-29 05:09:32
问题 I have a unit test done using moq to mock the objects, and the test is working fine, and now I want to use autofac +moq, but I'm having a few problems. this is the test: using (var mock = AutoMock.GetLoose()) { var issues = new List<Issue>(); issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 1 }); issues.Add(new Issue { Organization = "org", Repository = "repo", Number = 2 }); var numKeys = 0; mock.MockRepository.Create<IStorageService>() .Setup(myMock => myMock

When setting up a custom AutoDataAttribute for auto mocking, what's the proper syntax to tell AutoFixture to ignore all recursive structures?

吃可爱长大的小学妹 提交于 2021-01-29 03:09:46
问题 I have xUnit/Moq/AutoFixture successfully working together so that I am auto mocking objects via test method input parameters. I created a custom [AutoMoqData] attribute which I use on every test. Here's the code for the attribute: using System.Linq; using AutoFixture; using AutoFixture.AutoMoq; using AutoFixture.Xunit2; namespace Shared.TestResources.AutoFixture { public class AutoMoqDataAttribute : AutoDataAttribute { public AutoMoqDataAttribute() : base(() => new Fixture().Customize(new

MOQ- Setting up and verifying a generic method with Func argument

邮差的信 提交于 2021-01-28 19:34:21
问题 I have a third party interface which I want to mock its methods. To make my purpose clear consider the following IFoo interface which has a generic method like M2. One of M2 arguments is of type Func. public interface IFoo { bool M1<T>(); bool M2<T>(T arg, Func<T, string> func); } If I set up the M2 method as: var mock = new Mock<IFoo>(); mock.Setup(foo => foo.M2(It.IsAny<It.IsAnyType>(),It.IsAny<Func<It.IsAnyType, string>>())).Returns(true); mock.Object.M2("arg1", s => s); mock.Verify(foo =>

Moq-Setup fails when using myLambda() instead of myLambda.Invoke() on It.IsAny<>-MethodGroup

强颜欢笑 提交于 2021-01-28 05:22:36
问题 I use a Util-Method that is an Extension-method delaying It.*-calls until the Setup (using the result of an It.*-call stored in a variable doesn't seem to work). However, I noticed that when I call the Func<string> message with message() , the Setup will not work properly. When using message.Invoke() , it works just as I expect. As far as I understand, message() should be syntactic sugar for message.Invoke(), but then why do they behave differently? I do believe that has something to do with

how to Moq Fluent interface / chain methods

浪尽此生 提交于 2021-01-27 05:26:26
问题 I'm using the moq framework by Daniel Cazzulino, kzu Version 4.10.1. I want to moq so i can test a particular part of functionality (below is the simplistic version of the Code i could extract) The fluent/chain method so are designed so you can get object by an Id and include any additional information if required. i'm having some trouble fetching the correct object when the function is calling the moq'ed method, which is currently returning the last moq'ed object which is wrong /*My current

How to use Moq to mock up the StackExchange.Redis ConnectionMultiplexer class?

放肆的年华 提交于 2021-01-27 04:06:24
问题 I am working to mock up behaviors related to the StackExchange.Redis library, but can't figure out how to properly mock the sealed classes it uses. A specific example is in my calling code I'm doing something like this: var cachable = command as IRedisCacheable; if (_cache.Multiplexer.IsConnected == false) { _logger.Debug("Not using the cache because the connection is not available"); cacheAvailable = false; } else if (cachable == null) { The key line in there is _cache.Multiplexer

How to use Moq to mock up the StackExchange.Redis ConnectionMultiplexer class?

烈酒焚心 提交于 2021-01-27 04:05:09
问题 I am working to mock up behaviors related to the StackExchange.Redis library, but can't figure out how to properly mock the sealed classes it uses. A specific example is in my calling code I'm doing something like this: var cachable = command as IRedisCacheable; if (_cache.Multiplexer.IsConnected == false) { _logger.Debug("Not using the cache because the connection is not available"); cacheAvailable = false; } else if (cachable == null) { The key line in there is _cache.Multiplexer