moq-3

Mocking the behaviour of operators in Moq C#

橙三吉。 提交于 2020-03-22 09:21:51
问题 Hope your well. I am in the process of creating some tests using Moq in C#. One of the objects I am Mocking has overridden ==, > and < operators. Does anyone know if its possible, and if so how to... configure a Mock object to replicate this. The reason I ask is that I am trying to inject a mocked stub as some legacy code I have been given that has deep and dirty dependencies. Your time is appreciated Thanks 回答1: When you override such operations you should also provide theirs named

How to Mock ILogger / ILoggerService using Moq

这一生的挚爱 提交于 2020-01-03 08:22:21
问题 I'm writing some unit tests for my View Model class. The constructor of this class is injected with an ILoggerService. This interface defines 1 method GetLog which returns an ILogger. Something like below where this represents a class that implements ILoggable:- protected ViewModelBase(ILoggerService loggerService) { Logger = loggerService.GetLog(this); } I'm trying to unit test my CreateNewOrder method that looks like below: private void CreateNewOrder(INewOrderViewModel newOrderViewModel) {

how do you mock an xml for unit testing?

拟墨画扇 提交于 2019-12-21 12:31:11
问题 I need to unit testing this GetData method. public MessageResponse GetData(XmlElement requestElement) { MessageResponse MsgResponse = new MessageResponse(); if (requestElement.Attributes["employeeNo"] == null){ MsgResponse.Messages = new List<string>(); MsgResponse.Messages.Add("Attribute employeeNo is missing"); MsgResponse.Error = true; return MsgResponse; } if (requestElement.Attributes["xmlEmployeeName"] == null){ MsgResponse.Messages.Add("Attribute xmlEmployeeName is missing");

Original method still getting called in Moq even after CallBase = true/false

随声附和 提交于 2019-12-11 03:24:34
问题 Here's my code: public class Bar { } public class Foo { public string Name { get; set; } public Bar TheBar { get; set; } } public class Dependency { public Foo DoSomething(Expression<Func<Foo, bool>> exp1) { return new Foo(); } } public class Base { public Dependency Dependency { get; set; } public virtual Foo MethodA(Expression<Func<Foo, bool>> exp1, params Expression<Func<Foo, object>>[] exp2) { return Dependency.DoSomething(exp1); } } public class Derived : Base { public Foo DerviedMethod

how do you mock an xml for unit testing?

和自甴很熟 提交于 2019-12-04 06:38:00
I need to unit testing this GetData method. public MessageResponse GetData(XmlElement requestElement) { MessageResponse MsgResponse = new MessageResponse(); if (requestElement.Attributes["employeeNo"] == null){ MsgResponse.Messages = new List<string>(); MsgResponse.Messages.Add("Attribute employeeNo is missing"); MsgResponse.Error = true; return MsgResponse; } if (requestElement.Attributes["xmlEmployeeName"] == null){ MsgResponse.Messages.Add("Attribute xmlEmployeeName is missing"); MsgResponse.Error = true; return MsgResponse; } return MsgResponse; } this method needs a XmlElement parameter.

When using Moq Verify() method invocation count, have failing test's error message contain actual method invocation count using Moq

本小妞迷上赌 提交于 2019-12-01 15:44:10
Consider the following, where I am testing that an injected dependency's method is called a specific number of times: [Fact] public void WhenBossTalksEmployeeBlinksTwice() { // arrange var employee = new Mock<IEmployee>(); employee.Setup(e => e.Blink()); var boss = new Boss(employee.Object); // act boss.Talk(); // assert employee.Verify(e => e.Blink(), Times.Exactly(2)); // Passes as expected employee.Verify(e => e.Blink(), Times.Exactly(1)); // Fails as expected } When I force the failing test, the output is: Moq.MockException: Invocation was not performed on the mock 1 times: e => e.Blink()

When using Moq Verify() method invocation count, have failing test's error message contain actual method invocation count using Moq

此生再无相见时 提交于 2019-12-01 14:32:29
问题 Consider the following, where I am testing that an injected dependency's method is called a specific number of times: [Fact] public void WhenBossTalksEmployeeBlinksTwice() { // arrange var employee = new Mock<IEmployee>(); employee.Setup(e => e.Blink()); var boss = new Boss(employee.Object); // act boss.Talk(); // assert employee.Verify(e => e.Blink(), Times.Exactly(2)); // Passes as expected employee.Verify(e => e.Blink(), Times.Exactly(1)); // Fails as expected } When I force the failing

Verify value of reference parameter with Moq

南楼画角 提交于 2019-11-30 00:44:54
问题 I just switched to Moq and have run into a problem. I'm testing a method that creates a new instance of a business object, sets the properties of the object from user input values and calls a method (SaveCustomerContact ) to save the new object. The business object is passed as a ref argument because it goes through a remoting layer. I need to test that the object being passed to SaveCustomerContact has all of its properties set as expected, but because it is instantiated as new in the

AutoFixture with AutoMoq and concrete object injection

℡╲_俬逩灬. 提交于 2019-11-28 05:07:40
问题 I'm facing a strange problem related to AutoFixture and AutoMoqCustomization and how it deals with automocking of concrete classes. I suspect that I'm not using it very well but would like to know what's the problem. First of all her's some context. Let's say I have a class that I want to test : public class IdentityApplicationService { public IdentityApplicationService( TenantProvisioningService tenantProvisioningService) { // guard clause etc. _tenantProvisioningService =