C# Fluent Assertions global options for ShouldBeEquivalentTo - c#

In Fluent Assertions when comparing objects with DateTime properties there are sometimes a slight mismatch in the milliseconds and the comparison fail. The way we get around it is to set the comparison option like so:
actual.ShouldBeEquivalentTo(expected,
options =>
options.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation))
.WhenTypeIs<DateTime>());
Is there a way to set this up once and have it always apply instead of having to specify it every time we call ShouldBeEquivalentTo?
Update1:
Tried the following approach but it doesn't seem to work, test fails on 1 millisecond difference. The new default does not seem to get called by the factory.
using System;
using FluentAssertions;
using FluentAssertions.Equivalency;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
class Test
{
public DateTime TestDateTime { get; set; }
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void SettingFluentAssertionDefault()
{
// arrange
var defaultAssertionOptions = EquivalencyAssertionOptions<DateTime>.Default;
EquivalencyAssertionOptions<DateTime>.Default = () =>
{
var config = defaultAssertionOptions();
config.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs<DateTime>();
return config;
};
var testDateTime = DateTime.Now;
var expected = new Test {TestDateTime = testDateTime};
// act
var actual = new Test {TestDateTime = testDateTime.AddMilliseconds(1)};
// assert
actual.ShouldBeEquivalentTo(expected);
}
}
}

Now this can be done with the AssertionOptions static class. To use a simple example:
[TestInitialize]
public void TestInit() {
AssertionOptions.AssertEquivalencyUsing(options => options.ExcludingMissingMembers());
}
Or as in the example above:
AssertionOptions.AssertEquivalencyUsing(options =>
options.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs<DateTime>()
);

Actually, you can. The default configuration factory is exposed by the static property EquivalencyAssertionOptions<Test>.Default. You can easily assign an alternative configuration for a particular data type, or extend the default configuration with additional behavior. Something like:
var defaultAssertionOptions = EquivalencyAssertionOptions<Test>.Default;
EquivalencyAssertionOptions<Test>.Default = () =>
{
var config = defaultAssertionOptions();
config.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation)).WhenTypeIs<DateTime>();
return config;
};
If you want you can get the current default and tuck that away in some variable that you use from your factory method.

I am afraid the closest thing you can come to, is providing new methods
public static void ShouldBeEquivalentToDef<T>(this T subject, object expectation, string reason = "",
params object[] reasonArgs)
{
ShouldBeEquivalentToDef(subject, expectation, config => config, reason, reasonArgs);
}
public static void ShouldBeEquivalentToDef<T>(this T subject, object expectation,
Func<EquivalencyAssertionOptions<T>, EquivalencyAssertionOptions<T>> config, string reason = "", params object[] reasonArgs)
{
var context = new EquivalencyValidationContext
{
Subject = subject,
Expectation = expectation,
CompileTimeType = typeof (T),
Reason = reason,
ReasonArgs = reasonArgs
};
var defConstructedOptions = config(EquivalencyAssertionOptions<T>.Default());
defConstructedOptions.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation))
.WhenTypeIs<DateTime>()
new EquivalencyValidator(defConstructedOptions).AssertEquality(context);
}

Related

Setup argment matcher with IEnumerable in NSubstitue

I am working on a unit test for a project and I cannot figure out how to get NSubstitute to work the way I would expect it to. The issue I am having is that the code I was to substitute is in a while loop and depending on what is returned from the substituted value determines if the loop continues.
What I would like to do is have Process() return a different result based on what is passed in. I have tried
api.Process(Arg.Is<IEnumerable<int>>(new[] {1,2,3}, Arg.Any<bool>()).Returns(new ProcessingResult(){Success = true, IdsNotProcessed = List<int>{30}});
but it does not seems to work as processingResult comes back null because NSubstitue is not matching the argument.
[Test]
public void TestTwoLoops()
{
var api = Substitute.For<IApi>();
api.Process(/*list containing 1,2,3*/, Arg.Any<bool>()).Returns(new ProcessingResult(){Success = true, IdsNotProcessed = List<int>{30}});
api.Process(/*list containing 30*/, Arg.Any<bool>()).Returns(new List<int>{});
var sut = new WidgetMaker(api);
sut.MakeWidget();
}
public class WidgetMaker
{
public WidgetMaker(IApi api)
{
_api = api;
}
public void MakeWidgets(IEnumerable<int> widgetIds)
{
var idsToProcess = widgetIds.ToList();
while(true)
{
if(!idsToProcess.Any())
{
berak;
}
var processingResult = _api.Process(idsToProcess, false);
if(processingResult.Success)
{
idsToProcess.Clear();
idsToProcess.AddRange(processingResult.IdsNotProcessed);
}
else
{
break;
}
}
}
private IApi _api;
}
As I was writing this question, the answer came to me, but I have a feeling others might find this helpful.
Using the overload that accepts a predicate and using the SequenceEqualExtension method with a parameter of new[] {/values I want to be input/}
api.Process(Arg.Is<IEnumerable<int>>(x => x.SequenceEqual(new[] {1,2,3}, Arg.Any<bool>())).Returns(new ProcessingResult(){Success = true, IdsNotProcessed = List<int>{30}});

How to mock nested properties and objects and their functions?

I have the code below which I would like to test, but I'm not sure whether it is possible or not.
I have EF repositories and they are put together to a class as public properties. I don't know exactly whether it is bad solution or not, but it is easier to manage the code and its dependencies. Only the testability is still a question.
Purpose of my test is injecting data via
administrationRepository.ModuleScreen.GetAll()
method and catch the result. I know that it can be tested once it is deployed, but I want the tests in build time in order to have as fast feedback as possible.
I went through questions and answers here, but I cannot find answers. In my code I got to the point where the property is set up, but when I call the administrationRepoMock.Object.ModuleScreen.GetAll() ReSharper offers only the methods coming from Entitiy Framework and not the Moq related functions.
It is possible what I want? If so, how? Is my design suitable for this? If not can you give me articles, urls where I can see examples?
Repository:
public interface IModuleScreen
{
IEnumerable<DomainModel.Administration.ModuleScreen> GetAll();
}
public interface IAdministrationRepository
{
IModuleScreen ModuleScreen { get; }
}
public partial class AdministrationRepository : IAdministrationRepository
{
public virtual IModuleScreen ModuleScreen { get; private set; }
public AdministrationRepository( IModuleScreen moduleScreen )
{
this.ModuleScreen = moduleScreen;
}
}
Application:
public partial class DigitalLibraryApplication : IDigitalLibraryApplication
{
private IAdministrationRepository _administrationRepository;
private IMapper.IMapper.IMapper _mapper;
private IDiLibApplicationHelper _dilibApplicationHelper;
#region Ctor
public DigitalLibraryApplication( IAdministrationRepository administrationRepository, IMapper.IMapper.IMapper mapper, IDiLibApplicationHelper diLibApplicationHelper)
{
_administrationRepository = administrationRepository;
_mapper = mapper;
_dilibApplicationHelper = diLibApplicationHelper;
}
#endregion
public IEnumerable<ModuleScreenContract> GetModuleScreens()
{
//inject data here
IEnumerable<ModuleScreen> result = _administrationRepository.ModuleScreen.GetAll();
List<ModuleScreenContract> mappedResult = _mapper.MapModuleScreenToModuleScreenContracts(result);
return mappedResult;
}
}
Test code:
[Test]
public void ItCalls_ModuleRepository_Get_Method()
{
List<SayusiAndo.DiLib.DomainModel.Administration.ModuleScreen> queryResult = new List<SayusiAndo.DiLib.DomainModel.Administration.ModuleScreen>()
{
new DomainModel.Administration.ModuleScreen()
{
Id = 100,
},
};
var moduleScreenMock = new Mock<IModuleScreen>();
moduleScreenMock.Setup(c => c.GetAll()).Returns(queryResult);
administrationRepoMock.SetupProperty(c => c.ModuleScreen, moduleScreenMock.Object);
var mapperMock = new Mock<IMapper.IMapper.IMapper>();
var dilibApplicationHerlperMock = new Mock<IDiLibApplicationHelper>();
IDigitalLibraryApplication app = new DigitalLibraryApplication( administrationRepoMock.Object, mapperMock.Object, dilibApplicationHerlperMock.Object );
app.GetModules();
//issue is here
administrationRepoMock.Object.ModuleScreen.GetAll() //???
}
Here is a refactoring of your test that passes when run. You can update the pass criteria to suit you definition of a successful test.
[Test]
public void ItCalls_ModuleRepository_Get_Method() {
// Arrange
List<ModuleScreen> queryResult = new List<ModuleScreen>()
{
new ModuleScreen()
{
Id = 100,
},
};
//Building mapped result from query to compare results later
List<ModuleScreenContract> expectedMappedResult = queryResult
.Select(m => new ModuleScreenContract { Id = m.Id })
.ToList();
var moduleScreenMock = new Mock<IModuleScreen>();
moduleScreenMock
.Setup(c => c.GetAll())
.Returns(queryResult)
.Verifiable();
var administrationRepoMock = new Mock<IAdministrationRepository>();
administrationRepoMock
.Setup(c => c.ModuleScreen)
.Returns(moduleScreenMock.Object)
.Verifiable();
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(c => c.MapModuleScreenToModuleScreenContracts(queryResult))
.Returns(expectedMappedResult)
.Verifiable();
//NOTE: Not seeing this guy doing anything. What's its purpose
var dilibApplicationHerlperMock = new Mock<IDiLibApplicationHelper>();
IDigitalLibraryApplication app = new DigitalLibraryApplication(administrationRepoMock.Object, mapperMock.Object, dilibApplicationHerlperMock.Object);
//Act (Call the method under test)
var actualMappedResult = app.GetModuleScreens();
//Assert
//Verify that configured methods were actually called. If not, test will fail.
moduleScreenMock.Verify();
mapperMock.Verify();
administrationRepoMock.Verify();
//there should actually be a result.
Assert.IsNotNull(actualMappedResult);
//with items
CollectionAssert.AllItemsAreNotNull(actualMappedResult.ToList());
//There lengths should be equal
Assert.AreEqual(queryResult.Count, actualMappedResult.Count());
//And there should be a mapped object with the same id (Assumption)
var expected = queryResult.First().Id;
var actual = actualMappedResult.First().Id;
Assert.AreEqual(expected, actual);
}

How do I write my first serious test with MSpec?

I am new to testing and have never used MSpec. I looked at tutorials and the only examples is "lite", like 1 + 1 should be 2. I need to test this real method and I don't know where to start.
public ILineItem CreateLineItem(BaseVariationContent sku, int quantityToAdd)
{
var price = sku.GetDefaultPrice();
var parent = sku.GetParentProducts().FirstOrDefault() != null ? _contentLoader.Get<ProductContent>(sku.GetParentProducts().FirstOrDefault()).Code : string.Empty;
return new LineItem
{
Code = sku.Code,
DisplayName = sku.DisplayName,
Description = sku.Description,
Quantity = quantityToAdd,
PlacedPrice = price.UnitPrice.Amount,
ListPrice = price.UnitPrice.Amount,
Created = DateAndTime.Now,
MaxQuantity = sku.MaxQuantity ?? 100,
MinQuantity = sku.MinQuantity ?? 1,
InventoryStatus = sku.TrackInventory ? (int)InventoryStatus.Enabled : (int)InventoryStatus.Disabled,
WarehouseCode = string.Empty, // TODO: Add warehouse id
ParentCatalogEntryId = parent,
};
}
BaseVariationContent is just a class with a lot of properties and that has an extension.
The MSpec github repo has a pretty nice README that explains the basic syntax components of an MSpec test class and test case.
https://github.com/machine/machine.specifications#machinespecifications
I won't fill in the details of your test, but I will show you the important parts to setup an mspec test.
[Subject("Line Item")]
public class When_creating_a_basic_line_item_from_generic_sku()
{
Establish context = () =>
{
// you would use this if the Subject's constructor
// required more complicated setup, mocks, etc.
}
Because of = () => Subject.CreateLineItem(Sku, Quantity);
It should_be_in_some_state = () => Item.InventoryStatus.ShouldEqual(InventoryStatus.Enabled);
private static Whatever Subject = new Whatever();
private static BaseVariationContent Sku = new GenericSku();
private static int Quantity = 1;
private static ILineItem Item;
}
You'll want to run these tests, so use the command-line tool
https://github.com/machine/machine.specifications#command-line-reference
or one of the integrations
https://github.com/machine/machine.specifications#resharper-integration
Let me navigate to you with a real implementation.
Let's assume you have a service called SiteService and it returns the current siteId (you have multiple siteIds for your application).
you want to write a test case, when requesting the current site it should return the site id definition in configuration.
you will need to create a test class (standard class file), let's give a meaningful name like "SiteServiceSpec.cs"
Next, you will need to mock the ISiteConfiguration so that it can get the site id from the SiteConfiguration
public abstract class SiteServiceContext : WithFakes
{
Establish context = () =>
{
var siteConfiguration = An<ISiteConfiguration>();
siteConfiguration.WhenToldTo(x => x.Id)
.Return(CurrentSiteId);
Repository = An<IRepository<WebSite.Domain.Site.Site>>();
SUT = new SiteService(siteConfiguration, Repository);
};
protected const short CurrentSiteId = 1;
protected static SiteService SUT;
protected static IRepository<WebSite.Domain.Site.Site> Repository;
}
Now, here comes the example of the test class.
[Subject(typeof(SiteService))]
public class When_requesting_current_site : SiteServiceContext
{
It should_return_site_with_id_defined_in_configuration = () =>
Result.Id.ShouldEqual(CurrentSiteId);
Establish context = () =>
{
var site = An<WebSite.Domain.Site.Site>();
site.WhenToldTo(x => x.Id)
.Return(CurrentSiteId);
Repository.WhenToldTo(x => x.GetById(CurrentSiteId))
.Return(site);
};
Because of = () =>
Result = SUT.GetCurrentSite();
static WebSite.Domain.Site.Site Result;
}
I hope it will help you to get an idea of how it works. Besides, follow the structure provided by #anthony-mastrean

Setup for testing a Linq query on a LinqToExcel IExcelQueryFactory

Using c#, Moq, MSTest, LinqToExcel
I'm having trouble figuring out the best way to Setup() a mock for a response from a Linq query on a Linq-to-Excel IExcelQueryFactory.
I think I should expect the Linq Query to return something of type delegate, but I'm not quite sure what that should look like
Can anyone suggest what my Return() should look like in the Moq Setup() below?
Also, any toughts on my approach to testing and mocking these Lynq methods? Should I be approaching this differently?
Thanks! I'm going to go book up on delegates now. :)
The Test
[TestClass]
public class ThingsSheetTests
{
[TestMethod]
public void GetRows_ReturnsListOfThings()
{
// Arrange
var mockExcelQueryFactory = new Mock<IExcelQueryFactory>();
var thingsSheet = new ThingsSheet(mockExcelQueryFactory.Object, "file", "worksheet");
mockExcelQueryFactory
.Setup(x => x.Worksheet<Thing>(It.IsAny<string>))
// I think this is correctly casting to a delegate
// however ExelQuerable constructor needs arguments:
// public ExcelQueryable(IQueryProvider provider, Expression expression);
// looking into what kind of IQueryProvider and Expression I should supply.
.Returns(Action(() => new ExcelQueryable<Thing> { })); // getting closer!
// Act
thingsSheet.GetRows();
// Assert
mockExcelQueryFactory.Verify();
}
}
The Class and Method I'm testing
public class ThingsSheet
{
private string importFile;
private string worksheetName;
private IExcelQueryFactory excelQueryFactory;
public ThingsSheet(IExcelQueryFactory excelQueryFactory, string importFile, string worksheetName)
{
this.excelQueryFactory = excelQueryFactory;
this.importFile = importFile;
this.worksheetName = worksheetName;
this.AddMappings();
}
private void AddMappings()
{
excelQueryFactory.AddMapping<Thing>(t => t.Id, "Thing ID");
}
public List<Thing> GetRows()
{
excelQueryFactory.AddMapping<Thing>(t => t.Id, "Thing ID");
var things = from thing in excelQueryFactory.Worksheet<Thing>(this.worksheetName)
select new Thing { };
return things.ToList<Thing>();
}
}
You can use e.g. a method which returns your fake data.
mockExcelQueryFactory
.Setup(x => x.Worksheet<Thing>(It.IsAny<string>()))
.Returns(ExcelQueryableOfThing());
Lets say Thing class looks like this:
public class Thing
{
public string Id { get; set; }
public string Name { get; set; }
}
Then in the method ExcelQueryableOfThing() you have to mock the CreateQuery<TElement>(Expression expression) method of IQueryProvider provider. Something like this:
private ExcelQueryable<Thing> ExcelQueryableOfThing()
{
var things = new List<Thing>
{
new Thing
{
Id = "1",
Name = "Adam"
},
new Thing
{
Id = "1",
Name = "Eva"
}
}
.AsQueryable();
Mock<IQueryProvider> queryProvider = new Mock<IQueryProvider>();
queryProvider
.Setup(p => p.CreateQuery<Thing>(It.IsAny<Expression>()))
.Returns(() => things);
Expression expressionFake = Expression.Constant(new List<Thing>().AsQueryable());
return new ExcelQueryable<Thing>(queryProvider.Object, expressionFake);
}
Then in the unit test thingsSheet.GetRows() will return your fake data (Adam and Eva :). HTH
[TestMethod]
public void GetRows_ReturnsListOfThings()
{
// Arrange
Mock<IExcelQueryFactory> mockExcelFile = new Mock<IExcelQueryFactory>();
var thingsSheet = new ThingsSheet(mockExcelFile.Object, "file", "worksheet");
mockExcelFile
.Setup(x => x.Worksheet<Thing>(It.IsAny<string>()))
.Returns(ExcelQueryableOfThing());
// Act
List<Thing> rows = thingsSheet.GetRows();
// Assert
Assert.AreEqual(2, rows.Count); // Adam and Eva
}

Testing value of set-only property using only Moq

I have a simple class that sets a field in the constructor, and the matching property is read-only.
public class COMAssembly : ICOMAssembly
{
private List<string> properties = new List<string>();
public List<string> Properties
{
get { return properties; }
}
...
}
I have these test classes using MSpec and Moq:
using Machine.Specifications;
using Moq;
using System.Collections.Generic;
using It = Machine.Specifications.It;
public class MSPEC_With_a_COM_Assembly
{
protected static Mock<ICOMAssembly> _mockCOMAssembly;
protected static List<string> _listOfStrings;
Establish context = () =>
{
_mockCOMAssembly = new Mock<ICOMAssembly>(MockBehavior.Loose);
_mockCOMAssembly.DefaultValue = DefaultValue.Mock;
_mockCOMAssembly.SetupAllProperties();
_mockCOMAssembly.SetupProperty(m => m.Properties, new List<string>() { "Prop1", "Prop2" });
};
}
[Subject(typeof(MSPEC_With_a_COM_Assembly), "With a COM Assembly")]
public class When_asking_for_a_list_of_properties_and_assembly_has_properties : MSPEC_With_a_COM_Assembly
{
Because of = () =>
{
_listOfStrings = _mockCOMAssembly.Object.Properties;
};
It Should_return_a_list_with_values = () =>
{
//TODO: Verify that the Count property of _listOfStrings/_mockCOMAssembly.Object.Properties is greater than zero.
};
}
I've reviewed many forums, Moq tutorials, etc. and can't find an answer to how to do this using only Moq. I do not want to use NUnit. I have other tests that pass that verify that _listOfStrings is not null and the get on the property works fine. I'm new to practicing MSpec/Moq/Unit Testing, though I've read a lot on the topics so far. Any help is appreciated!

Categories