moq

Moq ReturnsAsync() with no parameters

随声附和 提交于 2019-12-13 13:00:20
问题 I use Moq. I have mocked a class which has method that looks like following: public async Task DoSomething() { // do something... } I setup it like below: SomeMock.Setup(x => x.DoSomething()) .Callback(() => ... )) .Returns(Task.FromResult(default(int))); I don't like last line: .Returns(Task.FromResult(default(int))) . Is there a way to setup async return in more elegant way. I know there is a method ReturnsAsync() but it has a parameter. My method returns just Task so I don't have a

NullReferenceException in UnitTest using Verifyable on an async Method [duplicate]

故事扮演 提交于 2019-12-13 08:30:12
问题 This question already has answers here : How can I tell Moq to return a Task? (4 answers) Using Moq to mock an asynchronous method for a unit test (3 answers) Closed 12 months ago . I just had a problem with setting up a Unit Test using xUnit and Moq . My Testee The Method TestMethod() contains the Logic, I want to test. It is async and though returns a Task public class Testee { private readonly IDoSomething _doSomething; public Testee(IDoSomething doSomething) { _doSomething = doSomething;

Mocking a protected member without reflection

和自甴很熟 提交于 2019-12-13 07:58:40
问题 I have a member that returns a string from a resource file, and I want to unit test this, as there's quite a few and they could be changed by mistake. I understand that this is achievable using reflection but I've been asked to do it in a way that doesn't use reflection. The members look something like this; protected override string StringOne { get { return Resources.String; } } I understand that setting up the return for the member can be done as; mock.Protected() .Setup<string>("StringOne"

Linq .FirstOrDefault() on a Mocked List

十年热恋 提交于 2019-12-13 07:57:14
问题 I'm trying to write a unit test using Moq for some code and coming up against a NullReferenceException on my call to firstOrDefault(). Here is a snippet of my affected code: [TestMethod] public void LinqAlist() { var _mockList = new Mock<List<int>>(); var realData = new List<int>() {1, 2, 3}; _mockList.Object.AddRange(realData); //returns 1 var realOne = realData.FirstOrDefault(x => x == 1); //throws NullReferenceException var mockOne = _mockList.Object.FirstOrDefault(x => x == 1); } I can't

Moq, unit test using xUnit framework and testing a function returning an object

江枫思渺然 提交于 2019-12-13 07:48:36
问题 I have a repository public class StudentsPersonalDetailsRepository : IStudentPersonalDetailsRepository { private readonly StudentManagementSystemEntities _studentsDbContext; private readonly ILogger _logger; public StudentsPersonalDetailsRepository(StudentManagementSystemEntities context, ILogger<IStudentPersonalDetailsRepository> logger) { _studentsDbContext = context; _logger = logger; } public IQueryable<StudentPersonalDetails> StudentPersonalDetails => _studentsDbContext

Using Moq to verify a Prism event subscription fails

馋奶兔 提交于 2019-12-13 05:13:17
问题 I am using the Prism framework together with Moq. I am trying to verify that the AlarmService subscribes to an event in the constructor but I am getting an exception that this is not supported. How else can I verify this? This is my testMethod: public void TestMethod() { var mockMachineDataService = new Mock<IMachineDataService<AlarmDto>>(); var mockAggregator = new Mock<IEventAggregator>(); var mockEvent = new Mock<MachineMessageReceivedEvent>(); mockAggregator.Setup(x => x.GetEvent

Unit testing with Moq, Prism 6, and Event Aggregation

我怕爱的太早我们不能终老 提交于 2019-12-13 02:35:49
问题 I want to unit test a module by throwing messages at it via Event Aggregation to make sure it responds appropriately, either by setting properties appropriately, or by publishing other messages as a result. I am using Prism 6. In my project, the infrastructure project has: public class ImportantMessage : PubSubEvent<string> { } ModuleA publishes a message like this: eventAggregator.GetEvent<ImportantMessage>().Publish(importantString); ModuleB receives the message like this: eventAggregator

Moq custom IIdentity

浪子不回头ぞ 提交于 2019-12-13 01:29:12
问题 I created a custom RoleProvider (standard webforms, no mvc) and I would like to test it. The provider itself integrates with a custom implementation of IIdentity (with some added properties). I have this at the moment: var user = new Mock<IPrincipal>(); var identity = new Mock<CustomIdentity>(); user.Setup(ctx => ctx.Identity).Returns(identity.Object); identity.SetupGet(id => id.IsAuthenticated).Returns(true); identity.SetupGet(id => id.LoginName).Returns("test"); // IsAuthenticated is the

How to resolve Castle.Windsor and MoQ version conflicts for Castle.Core assembly

99封情书 提交于 2019-12-12 18:33:24
问题 In my project I need to use simultaneously Castle.Windsor and Moq dlls. Windsor requires Castle.Core also to be referenced in the project. Problem starts when I try to use methods from Castle.Core: Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(...); Problem1: If I use Moq.dll from NET40 folder, I got built error "The type 'Castle.DynamicProxy.Generators.AttributesToAvoidReplicating' exists in both '...\Windsor\dotNet40\Castle.Core.dll' and '...\MoQ\NET40\Moq.dll'" Problem2:

Moq: Callback after method invocation on a method that does not return a value

半城伤御伤魂 提交于 2019-12-12 15:26:41
问题 I am currently using the Moq library for unit testing. Moq gives me the ability to register callbacks before and after method invocation on a mocked object like so: Mock<IMyClass> mock = new Mock<IMyClass>(); mock.Setup(o => o.MyMethod()) .Callback(() => Console.WriteLine("BEFORE!")) .Returns(true) .Callback(() => Console.WriteLine("AFTER!")); However, if MyMethod does not return a value (i.e. it has a void return type), then I can only setup a single Callback like so: mock.Setup(o => o