English 中文(简体)
Rhino Mocks - using AssertWasCalled on Common.Logging ILog.Debug
原标题:

I m having trouble using Rhino Mocks to assert that a method was called (and ideally with a particular parameter). The Method is ILog.Debug(FormatMessageHandler) in Common.Logging 2.0 using the new lamba syntax. It works fine using the old way plain ILog.Debug(string).

    // Sample Code to Test
    public int TestFuncLambda(ILog log, int a, int b)
    {
        log.Debug(m => m("TestFunc START"));

        int c = a + b;

        log.Debug(m => m("TestFunc END"));

        return c;
    }

    public int TestFunc(ILog log, int a, int b)
    {
        log.Debug("TestFunc START");

        int c = a + b;

        log.Debug("TestFunc END");

        return c;
    }

    [TestMethod]
    public void Should_log_start_TestFuncLamba()
    {
        var logger = MockRepository.GenerateMock<ILog>();

        logger.Stub(x => x.IsDebugEnabled).Return(true);

        TestFuncLambda(logger, 1, 2);

        // Doesn t work, says zero calls plus I m not sure how to check for the word "START" in the string either
        logger.AssertWasCalled(x => x.Debug(Arg<FormatMessageHandler>.Is.Anything), o => o.IgnoreArguments());
    }

    [TestMethod]
    public void Should_log_start_TestFunc()
    {
        var logger = MockRepository.GenerateMock<ILog>();
        logger.Stub(x => x.IsDebugEnabled).Return(true);

        TestFunc(logger, 1, 2);

        // Works fine
        logger.AssertWasCalled(x => x.Debug(Arg<string>.Matches(Text.Contains("START"))));
    }
最佳回答

I figured it out. I was missing the Action part for the delegate. The proper syntax is:

logger.AssertWasCalled(x => x.Debug(Arg<Action<FormatMessageHandler>>.Is.Anything));

rather than

logger.AssertWasCalled(x => x.Debug(Arg<FormatMessageHandler>.Is.Anything), o => o.IgnoreArguments());

As mentioned o.IgnoreArguments() was redundant and not necessary.

问题回答

I m going to assume here that you are just tinkering with Rhinomocks, and this has nothing to do with the logging framework, is that correct? I say this because there are no concrete implementations in your tests, only mocks.

Without testing your code, this line looks like it will always be zero:

logger.AssertWasCalled(x => x.Debug(Arg<FormatMessageHandler>.Is.Anything), o => o.IgnoreArguments());

because your actual method TestFunc() passes strings to log.Debug, and not a FormatMessageHandler:

So it would make sense that the number of calls is zero. Add a line to TestFunc() like this:

log.Debug(new FormatMessageHandler());

and see if that fixes it.

First, create a concrete class to see if the right Debug() method is being called in TestFuncLambda. This makes sure that it s not doing some sort of weird conversion of the lambda to string.

Once you verify that is should be calling the correct version, you ve isolated the problem with RhinoMocks. It could be a bug with rhino mocks. So, lets reduce the failure set but wrapping the lambda in a new FormatMessageHandler() before you pass it into Debug. This ensures that the right mocked function is being called and not translated as something else.

If you haven t found a bug at this point, and it still doesn t work, try creating an instance of FormatMessageHandler() and saving it as a static member variable (just to test what s wrong). Pass in that saved on in the TestFuncDebug call to Debug() and the AssertWasCalled() call. If that doesn t work, I m out of ideas.

BTW, I dont know what IgnoreArguments() is, but I never have to call it in my RhinoMocks calls to AssertWasCalled. Usually having Arg<>.Is.Anything works fine.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签