Skip to main content

Strongly-Typed Property Constraints in Rhino Mocks

UPDATE: As I suspected, this functionality was right in front of my face – it’s called PredicateConstraint in Rhino Mocks. I also realize that I managed to completely ignore the existence of Predicate<T> in the framework, and write my own predicate delegate. Hey, I was on a roll.


Rhino Mocks has a PropertyConstraint class that allows you to check the values on properties of objects passed into the method as part of the verification for that method being called. Unfortunately, the name of the property is specified as a string, which means the benefits of strong-typing that Rhino Mocks is normally so good at preserving are lost. Here’s an example (using Rhino Mocks 3.3 and .NET 2.0):

[Test]
public void Main_Form_Should_Show_Start_Panel_On_Load()
{
    MockRepository mockRepository = new MockRepository();
    IMainFormView mockView = mockRepository.DynamicMock<IMainFormView>();

    IEventRaiser loadEventRaiser = GetEventRaiserFor(delegate { mockView.Load += null; });
    Expect.Call(
        delegate
        {
            mockView.SetMainFormControlVisibility(null);
        }).Constraints(new PropertyConstraint("StartPanelIsVisible", Is.Equal(true)));
    mockRepository.ReplayAll();

    IMainFormController controllerUnderTest = new MainFormController(mockView);
    loadEventRaiser.Raise(mockView, EventArgs.Empty);

    mockRepository.VerifyAll();            
}

Basically, the view’s method SetMainFormControlVisibility takes an object with a property called StartPanelIsVisible. We want to make sure that when the controller calls this method, this property is set to true on the parameter object. In fact, that is the sole reason for the existence of this test. Using a string to specify the property name isn’t very conducive to refactoring, and it would be nice to have intellisense and compile-time argument checking available when we specify the constraints for an object’s state. I found myself writing this class:

public class PropertyConstraint<T> : AbstractConstraint where T : class
{
    public delegate bool ConfirmPropertyState<U>(U statefulObject);
    private readonly ConfirmPropertyState<T> confirmPropertiesMatch;

    public PropertyConstraint(ConfirmPropertyState<T> confirmPropertiesMatch)
    {
        this.confirmPropertiesMatch = confirmPropertiesMatch;
    }

    public override bool Eval(object obj)
    {
        AssertObjectIsCorrectType(obj);
        T objectToConfirmStateOf = Cast(obj);

        return confirmPropertiesMatch(objectToConfirmStateOf);
    }

    public override string Message
    {
        get { return String.Format(
            "The properties of the passed in {0} do not match the expectations of the constraint.", 
            typeof(T).FullName); }
    }

    private static void AssertObjectIsCorrectType(object obj)
    {
        if (!(obj is T))
        {
            throw new ArgumentException(
                String.Format(
                    "PropertyConstraint expected to evaluate an object of type {0} but was pass an object of type {1}",
                    typeof (T).FullName,
                    obj.GetType().FullName));
        }
    }

    private static T Cast(object obj)
    {
        return obj as T;
    }
}

Which changes our test to look like this:

[Test]
public void Main_Form_Should_Show_Start_Panel_On_Load()
{
    MockRepository mockRepository = new MockRepository();
    IMainFormView mockView = mockRepository.DynamicMock<IMainFormView>();

    IEventRaiser loadEventRaiser = GetEventRaiserFor(delegate { mockView.Load += null; });
    PropertyConstraint<MainFormControlVisibility> startPanelIsVisible = 
        new PropertyConstraint<MainFormControlVisibility>(
            delegate(MainFormControlVisibility match) 
            {
                return match.StartPanelIsVisible; 
            });
    Expect.Call(
        delegate
        {
            mockView.SetMainFormControlVisibility(null);
        }).Constraints(startPanelIsVisible);
    mockRepository.ReplayAll();

    IMainFormController controllerUnderTest = new MainFormController(mockView);
    loadEventRaiser.Raise(mockView, EventArgs.Empty);

    mockRepository.VerifyAll();
}   

I admit this is pretty clunky (it looks better with lambdas), but it achieves the object of getting rid of the string for property specification.

I’m almost sure something like this exists in Rhino Mocks as it is, but I couldn’t find it, so I thought I’d share this. Again, I’m using an older version of Rhino Mocks (3.3).

Comments

Popular posts from this blog

Migrating Hg Repos with hg-fast-export and Windows Subsystem for Linux

Introduction I prefer Mercurial (hg) to git . I don’t really have any reason for this preference - they both do the same thing, and the user experience for 90% of the use cases is the same. It probably comes from the conditions of the DVCS landscape when I started using these systems. Some of this may have been perception only, but it looked like this: GitHub didn’t have free private repos BitBucket did have free private repos BitBucket was very hg-friendly Joel Spolsky had an amazing tutorial that served as both a how-to for hg as well as a general intro to DVCS hg was much more Windows-friendly than git Since hg was written in python, I felt like extending it would be easier than doing so for git if I ever needed to (admittedly, this is a pretty ridiculous reason) hg felt like a more unified, “coherent” system than the very linux-y feeling git and its extensions (also pretty ridiculous) Where they differed, I liked the verbs hg used better than git’s counterparts ...

Enabling Globalization Invariant Mode for .NET Core App on Raspberry Pi Running LibreElec

I had an app I wanted to run on my Raspberry Pi 3 running LibreElec . In LibreElec you can install the dotnet core 2.2 runtime as an addon, and in Visual Studio you can compile for ARM processors with ‘Target Runtime’ set to ‘linux-arm’ in the publish profile. So, I published to a folder from VS using that profile, and I copied the output over to my RPi which had the dotnet runtime installed. I did a simple dotnet Whatever.dll to run the app (actually in this case, it was /storage/.kodi/addons/tools.dotnet-runtime/bin/dotnet Whatever.dll because of the way the addon is installed) and was met with this error: FailFast: Couldn't find a valid ICU package installed on the system. Set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support. at System.Environment.FailFast(System.String) at System.Globalization.GlobalizationMode.GetGlobalizationInvariantMode() at System.Globalization.GlobalizationMode..cctor() at Syste...

Stubbing Static Methods with PostSharp

TypeMock uses the Profiler API to allow mocking, stubbing, etc. of classes used by code under test. It has the ability to handle sealed classes, static classes, non-virtual methods, and other troublesome-yet-oft-encountered scenarios in the world of unit testing. Other frameworks rely on proxies to intercept method calls, limiting them to be able to only fake virtual, abstract, and interface members. They also rely on dependecy injection to place the proxies as the concrete implementation of calls to the abstracted interface members. Anyone working with a legacy codebase is bound to run into static method calls (especially in the data access layer), dependencies on concrete types with non-virtual methods, and sealed class dependencies (HttpContext anyone?). The only way to unit test this without refactoring is with TypeMock. I've never used TypeMock, and I'm sure it's a great product, but it's not free. I decided to spike some code to see if I could solve the prob...