Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created February 6, 2011 05:40
Show Gist options
  • Save beccasaurus/813165 to your computer and use it in GitHub Desktop.
Save beccasaurus/813165 to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
namespace NUnit.Framework {
public static class ShouldHavePropertiesExtension {
/// <summary>For checking lots of properties on an object</summary>
/// <remarks>
/// Lets you say:
/// <code>
/// foo.ShouldHaveProperties(new {
/// Id = "This",
/// Foo = 15.89,
/// Hi = "Something"
/// });
/// </code>
///
/// Instead of having to say:
/// <code>
/// foo.Id.ShouldEqual("This");
/// foo.Foo.ShouldEqual(15.89);
/// foo.Hi.ShouldEqual("Something");
/// </code>
/// </remarks>
public static void ShouldHaveProperties(this object o, object anonymousObjectOfAttributes) {
var type = o.GetType();
foreach (var propertyItem in anonymousObjectOfAttributes.ToDictionary()) {
var property = type.GetProperty(propertyItem.Key);
var expected = propertyItem.Value;
var actual = property.GetValue(o, new object[] {});
actual.ShouldEqual(expected);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment