Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a generic test method to ensure that a class has a subset of properties of a separate class?
    primarykey
    data
    text
    <p>I am endeavoring to write a test method to determine if a class has a superset of properties of a different class. This is for purposes of mapping from domain objects to view model objects and as such there is no relationship between the two classes.</p> <p>As an example if I had the domain class <code>Foo</code> and the view model class <code>FooMap</code> I would want to test that <code>Foo</code> had the properties that <code>FooMap</code> expected it to.</p> <pre><code>public class Foo { public string Bar { get;set; } public string Baz { get;set; } public string NoOneCaresProp { get; set; } public string GetBarBaz() { return Bar + Baz; } } public class FooMap { public string Bar { get; set; } public string GetBarBaz { get; set; } public string NotAFooProp { get; set; } } </code></pre> <p>Given the properties of <code>FooMap</code> for the purposes of this test I want to ensure that class <code>Foo</code> has the <code>Bar</code> property and the <code>GetBarBaz</code> method. Additional methods or properties for either class should be ignored. I've written the following static method to perform such a test but am not happy with my implementation:</p> <pre><code>public static void ExpectedPropertiesExist&lt;TSource, TDestination, R&gt;(params Expression&lt;Func&lt;TDestination, R&gt;&gt;[] exclude) { var excludedProperties = exclude.Select(e =&gt; (e.Body as MemberExpression).Member.Name); var mappedProperties = typeof(TDestination).GetProperties() .Select(p =&gt; p.Name) .Except(excludedProperties); var sourceType = typeof(TSource); var baseTypeNames = sourceType.GetProperties().Select(b =&gt; b.Name).ToList(); baseTypeNames.AddRange(sourceType.GetMethods().Select(b =&gt; b.Name)); Assert.IsTrue(new HashSet&lt;string&gt;(baseTypeNames) .IsSupersetOf(mappedProperties)); } </code></pre> <p>The code to call the above is not nearly as succinct as I would like it to be: </p> <pre><code>// what my call to the function looks like now TestExtensionFun.ExpectedPropertiesExist&lt;Foo, FooMap, object&gt;(fm =&gt; fm.NotAFooProp); // I'd prefer it to look like this TestExtensionFun.ExpectedPropertiesExist&lt;Foo, FooMap&gt;(fm =&gt; fm.NotAFooProp); </code></pre> <p>Nor am I sure that the method is as proficient as it could be. What is the best mechanism to write a generic test method to ensure that a class has a subset of properties of a separate class?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload