views:

129

answers:

1

I'm testing an extension method on an interface 'ISomeInterface'. The extension method actually calls to another method in the interface.

How do I set up a mock and the appropriate expectations (i want to set the expectation that 'SomeMethod', which is an defined method in the interface with a different signature then this extension method, will be called with 'someDifferentParam')

// Extension Method
public static ISomeInterface SomeMethod(this ISomeInterface someInterface, string someParam)
{
    // do work, then call the defined method in the interface
    someInterface.SomeMethod(int someDifferentParam)

    return someInterface;
}


// tried to do the following but it errors
[Test]
public void SomeMethod_WithSomeInterface_CallsOtherSomeMethod()
{
    const string someParam = "something";
    const int someOtherParam = 1;

    var mock = MockRepository.GenerateMock<ISomeInterface>();

    mock.SomeMethod(someParam);

    mock.AssertWasCalled(x => x.SomeMethod(someOtherParam));
}

EDIT

I finally got it working, but I'm open to suggestions/criticism. I'm just learning the ins/outs of Rhino Mocks myself =)

here is the real thing that I was testing. Since you can't compare two NHibernate.Criterion.Order objects by doing an Is.Equal or Is.SameAs I had to capture the arguments passed to the method call and then Assert on the ToString of the Order object because there are no public properties exposed on it.

// Extension Method
public static class NHibernateExtensions
{
    public static ICriteria AddOrder(this ICriteria criteria, params OrderBy[] orderBys)
    {
        foreach (var b in orderBys)
        {
            var arr = b.Property.Split(',');
            for (var i = 0; i < arr.Length; i++)
            {
                criteria.AddOrder(b.Direction == OrderDirection.Ascending
                                      ? Order.Asc(arr[i])
                                      : Order.Desc(arr[i]));
            }
        }

        return criteria;
    }
}


// here is the test that works
[TestFixture]
public class NHibernateExtensionsTester : TestBase
{
    [Test]
    public void AddOrder_1OrderBy_CallsAddOrderOnICriteriaWithCorrectOrder()
    {
        const string testProperty = "SomeProperty";
        var expected = (Order.Asc(testProperty)).ToString();

        var orderBys = new[]
                       {
                           new OrderBy
                           {
                               Direction = OrderDirection.Ascending,
                               Property = testProperty
                           }
                       };

        var mockCriteria = M<ICriteria>();

        mockCriteria.AddOrder(orderBys);

        var orderArgument = (Order)((mockCriteria.GetArgumentsForCallsMadeOn(x => x.AddOrder(null)))[0][0]);
        Assert.That(orderArgument.ToString(), Is.EqualTo(expected));
    }
}
+3  A: 

Jon, since in effect you're just testing your extension method I see nothing wrong and this. I tried your code out and worked fine for me. Here is the exact code that I ran (using xunit.net and TD.Net). The test passed.

What error are you getting?

George Mauer
i think my issue is not with the test, but with reference types, someOtherParam is a reference type (not an int like in my example above)
Jon Erickson
I got it to work, I had to pull the argument out with mock.GetArgumentsForCallsMadeOn(x => x.SomeMethod(null))
Jon Erickson