Quantcast
Channel: Fluent Assertions
Viewing all articles
Browse latest Browse all 1402

New Post: Mappings in object graph comparisons

$
0
0
Hi

I have written an extension for property mapping that can be used like this:
result.ShouldBeEquivalentTo(expected, opt => opt.Mapping<YType>(x => x.Foo, y => y.Bar));
publicclass EquivalencyAssertionOptions<TSubject>
    {
        public EquivalencyAssertionOptions<TSubject> Mapping<TMatch>(
            Expression<Func<TSubject, object>> propertyFromExpression, Expression<Func<TMatch, object>> propertyToExpression)
        {
            this.Using(new MatchByMappingRule(ExpressionExtensions.GetPropertyPath(propertyFromExpression), ExpressionExtensions.GetPropertyPath(propertyToExpression)));
            returnthis;
        }
    }
    
    publicclass MatchByMappingRule : IMatchingRule
    {
        privatereadonlystring _propertyPathSubject;

        privatereadonlystring _propertyPathExpectation;

        public MatchByMappingRule(string propertyPathSubject, string propertyPathExpectation)
        {
            _propertyPathSubject = propertyPathSubject;
            _propertyPathExpectation = propertyPathExpectation;
        }

        public PropertyInfo Match(PropertyInfo subjectProperty, object expectation, string propertyPath)
        {
            if (subjectProperty.Name.Equals(_propertyPathSubject))
            {
                return expectation.GetType().GetProperty(_propertyPathExpectation);
            }

            returnnull;
        }

        publicoverridestring ToString()
        {
            returnstring.Format("Mapping property {0} to {1}", _propertyPathSubject, _propertyPathExpectation);
        }
    }
But I like the proposed syntax more :)
.Where(dto => dto.Product.ManufacturerName).ShouldMapTo(o => o.Product.Manufacturer.Name)

Viewing all articles
Browse latest Browse all 1402

Trending Articles