Skip to content

Instantly share code, notes, and snippets.

@darrencauthon
Forked from JamesEggers1/AutoMapperIssue.cs
Created September 11, 2010 12:50
Show Gist options
  • Save darrencauthon/575156 to your computer and use it in GitHub Desktop.
Save darrencauthon/575156 to your computer and use it in GitHub Desktop.
public class ObjectUnderTest
{
private readonly MappingEngine mappingEngine;
public ObjectUnderTest(string prefix)
{
var configuration = new Configuration(new TypeMapFactory(), MapperRegistry.AllMappers());
configuration.CreateMap<Person, PersonModel>()
.ForMember(x => x.Name, y => y.MapFrom(orig => prefix + orig.Name));
mappingEngine = new MappingEngine(configuration);
}
public PersonModel MapObject(Person input)
{
return mappingEngine.Map<Person, PersonModel>(input);
}
}
public class Person
{
public string Name { get; set; }
}
public class PersonModel
{
public string IDoNotExistInPerson { get; set; }
public string Name { get; set; }
}
[TestFixture]
public class TestClass
{
[Test]
public void Test1()
{
var test = new ObjectUnderTest(null);
var result = test.MapObject(new Person {Name = "world"});
result.Name.ShouldEqual("world");
}
[Test]
public void Test2()
{
var test = new ObjectUnderTest("Hello ");
var result = test.MapObject(new Person {Name = "world"});
result.Name.ShouldEqual("Hello world");
}
}
@darrencauthon
Copy link
Author

Here's another way to potentially do what I think you're trying to do. You'll have to internalize your use of AutoMapper inside your ObjectUnderTest if you're going to use it that way, the static method will not help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment