views:

132

answers:

1

I got this test:

[Fact]
public void EverythingIsMappedJustFine(){
  new AutoMapperTask().Execute();
  Mapper.AssertConfigurationIsValid();
}

It throws a bit strange exception:

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)

Unfortunately - I couldn't reproduce this on smaller scale and can't figure out what exactly is going on.

What is coercion operator?


This might be useful. But I'm failing to extract and dumb down necessary information bits.

+2  A: 

I still don't know what exactly is coercion operator, but at least - I solved my problem found reason.

After some automapper debugging was able to reproduce problem:

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{}
}

Quite simple as it turns out - source has method which is named just like destination property.

Arnis L.