Created
November 27, 2012 04:37
-
-
Save miso-soup/4152408 to your computer and use it in GitHub Desktop.
Automapper の AssertConfigurationIsValid の確認
This file contains 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
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>()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AutoMapper http://automapper.codeplex.com/ の
AssertConfigurationIsValid の挙動の確認コードです。