我得到了这个测试:
[Fact]
public void EverythingIsMappedJustFine(){
new AutoMapperTask().Execute();
Mapper.AssertConfigurationIsValid();
}
它引发了一个奇怪的异常:
Test ‘Unit.Web.Bootstrap.AutoMapperFacts.EverythingIsMappedJustFine’ failed:
System.InvalidOperationException : No coercion operator is defined
between types ‘System.Void’ and ‘System.Object’.
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
…
at AutoMapper.DelegateFactory.CreateGet(MethodInfo method)
不幸的是 – 我无法在较小规模上重现这一点,也无法弄清楚到底发生了什么.
什么是强制操作符?
This可能有用.但我没有提取和愚弄必要的信息位.
解决方法:
我仍然不知道究竟是什么强制运算符,但至少 – 我解决了我的问题找到了原因.
经过一些自动化调试后,能够重现问题:
namespace mappertest
{
using AutoMapper;
using NUnit.Framework;
[TestFixture]
public class FooFacts
{
[Test]
public void MapToFizz()
{
Mapper.Initialize(c => c.AddProfile(new FooProfile()));
var foo = new Foo { Bar = "BarValue" };
var fooModel = Mapper.Map<Foo, FooModel>(foo);
Assert.AreEqual("BarValue", fooModel.Bar);
}
}
public class FooProfile : Profile
{
protected override void Configure()
{
CreateMap<Foo, FooModel>();
}
}
public class Foo
{
public string Bar { get; set; }
public void Fizz() { }
}
public class FooModel
{
public string Bar { get; set; }
public FizzModel Fizz { get; set; }
}
public class FizzModel { }
}
事实证明非常简单 – source有一个方法,就像目标属性一样命名.