Skip to content

Instantly share code, notes, and snippets.

@colin-young
Last active July 17, 2018 11:43
Show Gist options
  • Save colin-young/a6fc2dbb5a2d39e4abd0a35600ca59ad to your computer and use it in GitHub Desktop.
Save colin-young/a6fc2dbb5a2d39e4abd0a35600ca59ad to your computer and use it in GitHub Desktop.
Automapper Dictionary Mapping Problem
using AutoMapper;
using FluentAssertions;
using System.Collections.Generic;
using Xunit;
namespace Reporter.Tests
{
public class TestType
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
public class TestClass<T> where T : new()
{
public TestClass()
{
Mapper.Initialize(cfg => cfg.CreateMap<Dictionary<string, string>, T>());
}
public T GetMappedObject(string prop1, string prop2)
{
return Mapper.Map<T>(new Dictionary<string, string>{
{ "Prop1", prop1 },
{ "Prop2", prop2 }
});
}
}
public class AutomapperTests
{
[Fact]
public void TestMapping()
{
var mapper = new TestClass<TestType>();
var result = mapper.GetMappedObject("value1", "value2");
result.Prop1.Should().Be("value1");
result.Prop2.Should().Be("value2");
}
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0"/>
<PackageReference Include="xunit" Version="2.2.0"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0"/>
<PackageReference Include="FakeItEasy" Version="3.4.2"/>
<PackageReference Include="FluentAssertions" Version="4.19.3"/>
<PackageReference Include="AutoMapper" Version="6.1.1"/>
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}"/>
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment