Skip to content

Instantly share code, notes, and snippets.

@miso-soup
Created November 27, 2012 04:37
Show Gist options
  • Save miso-soup/4152408 to your computer and use it in GitHub Desktop.
Save miso-soup/4152408 to your computer and use it in GitHub Desktop.
Automapper の AssertConfigurationIsValid の確認
public class Person
{
public int Id { get; set; }
public String Name { get; set; }
}
public class PersonView
{
public int Id { get; set; }
public String NameText { get; set; }
}
public class TestClass
{
/// <summary>
/// マッピングのテスト
/// </summary>
[Test]
public void MappingTest()
{
//config
Mapper.CreateMap<Person, PersonView>()
.ForMember(
dst => dst.NameText,
opt => opt.MapFrom(src => src.Name));
//assert
Assert.DoesNotThrow(() => Mapper.AssertConfigurationIsValid());
//map
var model = new Person() { Id = 2, Name = "太郎" };
PersonView view = Mapper.Map<Person, PersonView>(model);
//assert
Assert.AreEqual(model.Id, view.Id);
Assert.AreEqual(model.Name, view.NameText);
}
/// <summary>
/// 設定不足で、AssertConfigurationIsValid でエラーが起きる場合
/// </summary>
[Test]
public void ConfigFail()
{
var model = new Person() { Id = 2, Name = "太郎" };
//config
Mapper.CreateMap<Person, PersonView>();
//NameTextに対応するプロパティが設定されていないので、AutoMapperConfigurationException
//が throw される。
Assert.That(() => Mapper.AssertConfigurationIsValid(),
Throws.InstanceOf<AutoMapperConfigurationException>());
}
}
@miso-soup
Copy link
Author

AutoMapper http://automapper.codeplex.com/ の
AssertConfigurationIsValid の挙動の確認コードです。

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