Skip to content

Commit 405304b

Browse files
committed
no need to throw to report errors
1 parent 956975b commit 405304b

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

docs/source/Configuration-validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ To skip validation altogether for this map, use `MemberList.None`. That's the de
5555

5656
## Custom validations
5757

58-
You can add custom validations through an extension point. See [here](https://github.com/AutoMapper/AutoMapper/blob/bdc0120497d192a2741183415543f6119f50a982/src/UnitTests/CustomValidations.cs#L42).
58+
You can add custom validations through an extension point. See [here](https://github.com/AutoMapper/AutoMapper/blob/master/src/UnitTests/CustomValidations.cs).

src/UnitTests/ConfigurationValidation.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ public class Source
55
{
66
public decimal Foo { get; set; }
77
}
8-
98
public class Destination
109
{
1110
public Type Foo { get; set; }
1211
public string Bar { get; set; }
1312
}
14-
15-
protected override MapperConfiguration CreateConfiguration() =>
16-
new(cfg => { cfg.CreateMap<Source, Destination>(); });
17-
13+
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<Source, Destination>());
1814
[Fact]
1915
public void Should_throw_unmapped_member_and_mismatched_type_exceptions()
2016
{
@@ -35,24 +31,19 @@ public void Should_throw_unmapped_member_and_mismatched_type_exceptions()
3531
);
3632
}
3733
}
38-
3934
public class When_testing_a_dto_with_mismatches_in_multiple_children : AutoMapperSpecBase
4035
{
4136
public class Source
4237
{
4338
public Type Foo { get; set; }
4439
public Type Bar { get; set; }
4540
}
46-
4741
public class Destination
4842
{
4943
public int Foo { get; set; }
5044
public int Bar { get; set; }
5145
}
52-
53-
protected override MapperConfiguration CreateConfiguration() =>
54-
new(cfg => { cfg.CreateMap<Source, Destination>(); });
55-
46+
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<Source, Destination>());
5647
[Fact]
5748
public void Should_throw_for_both_mismatched_children()
5849
{
@@ -101,7 +92,7 @@ private ComplexType(int someMember)
10192
});
10293

10394
[Fact]
104-
public void Should_fail_validation() => new Action(AssertConfigurationIsValid).ShouldThrowException<AutoMapperConfigurationException>(ex=>
95+
public void Should_fail_validation() => new Action(AssertConfigurationIsValid).ShouldThrowException<AutoMapperConfigurationException>(ex =>
10596
ex.MemberMap.ToString().ShouldBe("Void .ctor(ComplexType), parameter myComplexMember"));
10697
}
10798

@@ -204,7 +195,7 @@ public Dest(int value)
204195
public int Value { get; }
205196
}
206197

207-
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<Source, Dest>().ForCtorParam("value", o=>o.MapFrom(s=>4)));
198+
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<Source, Dest>().ForCtorParam("value", o => o.MapFrom(s => 4)));
208199

209200
[Fact]
210201
public void Should_map() => Mapper.Map<Dest>(new Source()).Value.ShouldBe(4);
@@ -384,7 +375,7 @@ class ValueConverter : IValueConverter<int, int>
384375
{
385376
cfg.CreateMap<Source, Destination>(MemberList.Source)
386377
.ForMember(d => d.ResolvedDest, o => o.MapFrom<MemberResolver, int>("Resolved"))
387-
.ForMember(d=>d.TypedResolvedDest, o => o.MapFrom<MemberResolver, int>(s => s.TypedResolved))
378+
.ForMember(d => d.TypedResolvedDest, o => o.MapFrom<MemberResolver, int>(s => s.TypedResolved))
388379
.ForMember(d => d.ConvertedDest, o => o.ConvertUsing<ValueConverter, int>("Converted"))
389380
.ForMember(d => d.TypedConvertedDest, o => o.ConvertUsing<ValueConverter, int>(s => s.TypedConverted));
390381
});
@@ -409,8 +400,8 @@ class Destination
409400
{
410401
public string OtherValue { get; set; }
411402
}
412-
protected override MapperConfiguration CreateConfiguration() => new(c=>c.CreateMap<Source, Destination>(MemberList.Source)
413-
.ForMember(d=>d.OtherValue, o=>o.MapFrom(s=>s.Value ?? "")));
403+
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateMap<Source, Destination>(MemberList.Source)
404+
.ForMember(d => d.OtherValue, o => o.MapFrom(s => s.Value ?? "")));
414405
[Fact]
415406
public void Should_be_ignored() => new Action(AssertConfigurationIsValid)
416407
.ShouldThrow<AutoMapperConfigurationException>().Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Source.Value));
@@ -515,10 +506,10 @@ public class OtherDest
515506
protected override void Because_of()
516507
{
517508
try
518-
{
519-
AssertConfigurationIsValid();
509+
{
510+
AssertConfigurationIsValid();
520511
}
521-
catch (AutoMapperConfigurationException ex)
512+
catch(AutoMapperConfigurationException ex)
522513
{
523514
_exception = ex;
524515
}
@@ -559,7 +550,7 @@ protected override void Because_of()
559550
{
560551
AssertConfigurationIsValid();
561552
}
562-
catch (AutoMapperConfigurationException ex)
553+
catch(AutoMapperConfigurationException ex)
563554
{
564555
_exception = ex;
565556
}

src/UnitTests/CustomValidations.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Should_call_the_validator()
3838
cfg.CreateMap<Source, Dest>();
3939
});
4040

41-
config.AssertConfigurationIsValid();
41+
new Action(config.AssertConfigurationIsValid).ShouldThrow<AutoMapperConfigurationException>().Message.ShouldBe(nameof(When_using_custom_validation));
4242

4343
_calledForRoot.ShouldBeTrue();
4444
_calledForValues.ShouldBeTrue();
@@ -55,6 +55,7 @@ private void Validator(ValidationContext context)
5555
context.Types.DestinationType.ShouldBe(typeof(Dest));
5656
context.ObjectMapper.ShouldBeNull();
5757
context.MemberMap.ShouldBeNull();
58+
context.Exceptions.Add(new AutoMapperConfigurationException(nameof(When_using_custom_validation)));
5859
}
5960
else
6061
{

0 commit comments

Comments
 (0)