Last active
November 15, 2018 15:48
-
-
Save rheone/c0f51753881bdf43b82fc7309576d5fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AutoMapper; | |
using Xunit; | |
namespace AutomapperIssue.ReadOnly | |
{ | |
public class AutoMapperTests | |
{ | |
[Fact] | |
public void AutoMapperValueResolverSilentlyFailsToSetValueTest() | |
{ | |
// Arrange | |
var mapper = new MapperConfiguration(_ => | |
{ | |
_.CreateMap<Source, IDestination>() | |
.ForMember(d => d.ValueFromValueResolver, o => o.ResolveUsing<DestinationValueResolver>()); | |
_.CreateMap<Source, IDestination>() | |
.As<Destination>(); | |
}).CreateMapper(); | |
var source = new Source | |
{ | |
Id = 42 | |
}; | |
// Act | |
var destination = mapper.Map<IDestination>(source); | |
// Assert | |
Assert.NotNull(destination.ValueFromValueResolver); // expecting destination.ValueFromValueResolver to be set via DestinationValueResolver | |
} | |
} | |
public class Source | |
{ | |
public int Id { get; set; } | |
} | |
public interface IDestination | |
{ | |
string ValueFromValueResolver { get; } | |
} | |
public class Destination : IDestination | |
{ | |
public string ValueFromValueResolver { get; set; } | |
} | |
public class DestinationValueResolver : IValueResolver<Source, IDestination, string> | |
{ | |
public string Resolve(Source source, | |
IDestination destination, | |
string destMember, | |
ResolutionContext context) | |
{ | |
return $"Value from source \"{source.Id}\"."; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment