Skip to content

Instantly share code, notes, and snippets.

@brenovieira
Last active August 29, 2015 14:27
Show Gist options
  • Save brenovieira/41994109029899a7b488 to your computer and use it in GitHub Desktop.
Save brenovieira/41994109029899a7b488 to your computer and use it in GitHub Desktop.
using Xunit;
using Should;
using AutoMapper;
namespace UnitTestProject1
{
public class InitializeTest : AutoMapperSpecBase
{
const string SomeId = "someId";
const string SomeOtherId = "someOtherId";
private Source _source;
private Destination _destination;
class Source
{
public string AccountId { get; set; }
}
class Destination
{
public string UserId { get; set; }
}
protected override void Establish_context()
{
Mapper.Initialize(cfg =>
{
cfg.ReplaceMemberName("Account", "User");
cfg.ReplaceMemberName("User", "Account");
cfg.CreateMap<Source, Destination>().ReverseMap();
});
}
protected override void Because_of()
{
_source = Mapper.Map<Destination, Source>(new Destination
{
UserId = SomeId
});
_destination = Mapper.Map<Source, Destination>(new Source
{
AccountId = SomeOtherId
});
}
[Fact]
public void Configuration_should_be_valid()
{
Mapper.AssertConfigurationIsValid(); //it doesn't work
}
[Fact]
public void Should_work_together()
{
_source.AccountId.ShouldEqual(SomeId); //it works
_destination.UserId.ShouldEqual(SomeOtherId); //it works
}
}
}
using Xunit;
using Should;
using AutoMapper;
namespace UnitTestProject1
{
public class ProfileTest : AutoMapperSpecBase
{
const string SomeId = "someId";
const string SomeOtherId = "someOtherId";
private Source _source;
private Destination _destination;
class Source
{
public string AccountId { get; set; }
}
class Destination
{
public string UserId { get; set; }
}
class MyProfile : Profile
{
protected override void Configure()
{
ReplaceMemberName("Account", "User");
ReplaceMemberName("User", "Account");
CreateMap<Source, Destination>().ReverseMap();
}
public override string ProfileName { get { return "MyProfile"; } }
}
protected override void Establish_context()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<MyProfile>();
});
}
protected override void Because_of()
{
_source = Mapper.Map<Destination, Source>(new Destination
{
UserId = SomeId
});
_destination = Mapper.Map<Source, Destination>(new Source
{
AccountId = SomeOtherId
});
}
[Fact]
public void Configuration_should_be_valid()
{
Mapper.AssertConfigurationIsValid(); //it doesn't work
}
[Fact]
public void Should_work_together()
{
_source.AccountId.ShouldEqual(SomeId); //it doesn't work (_source.AccountId == null)
_destination.UserId.ShouldEqual(SomeOtherId); //it works
}
}
}
using Xunit;
using Should;
using AutoMapper;
namespace UnitTestProject1
{
public class RegularTest : AutoMapperSpecBase
{
const string SomeId = "someId";
const string SomeOtherId = "someOtherId";
private Source _source;
private Destination _destination;
class Source
{
public string AccountId { get; set; }
}
class Destination
{
public string UserId { get; set; }
}
protected override void Establish_context()
{
Mapper.Configuration.ReplaceMemberName("Account", "User");
Mapper.Configuration.ReplaceMemberName("User", "Account");
Mapper.CreateMap<Source, Destination>().ReverseMap();
}
protected override void Because_of()
{
_source = Mapper.Map<Destination, Source>(new Destination
{
UserId = SomeId
});
_destination = Mapper.Map<Source, Destination>(new Source
{
AccountId = SomeOtherId
});
}
[Fact]
public void Configuration_should_be_valid()
{
Mapper.AssertConfigurationIsValid(); //it doesn't work
}
[Fact]
public void Should_work_together()
{
_source.AccountId.ShouldEqual(SomeId); //it works
_destination.UserId.ShouldEqual(SomeOtherId); //it works
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment