English 中文(简体)
Invoke callback in Moq
原标题:

I have a method that performs an asynchronous service call. I call this class by passing in the callback.

public void GetRights(EventHandler<GetRightsCompletedEventArgs> callback)
{
    ServiceClient client = new ServiceClient();
    client.GetRightsCompleted += new EventHandler<GetRightsCompletedEventArgs>(callback);
    client.GetRightsAsync();
}

GetRights(GetRightsCallback);

I m creating tests with MSTest, and I ve mocked the containing class (IGetRightsProxy) in Moq. How can I invoke the callback when this method is called in the test?

GetRightsForCurrentUserCompletedEventArgs results = 
    new GetRightsCompletedEventArgs(
    new object[] { new ObservableCollection<Right>()}, null, false, null);
Mock<IGetRightsProxy> MockIGetRightsProxy = new Mock<GetRightsProxy>();
最佳回答

One way of doing what I want is to extend the class like this:

class MockGetRightsProxy : IGetRightsProxy
{
    public void GetRights(EventHandler<GetRightsCompletedEventArgs> callback)
    {
        // Create some args here
        GetRightsCompletedEventArgs args = new GetRightsCompletedEventArgs(
            new object[] { new ObservableCollection<Right>() }, null, false, null);

        callback(null, args);
    }

}

I was looking for ways to invoke the callback in Moq, but this works, too.

问题回答

You want to be looking at Moq s Callback() extension on your mock;

Mock<IGetRightsProxy> mock = new Mock<IGetRightsProxy>();
mock.Setup(x => x.GetRights(It.IsAny<EventHandler<GetRightsCompletedEventArgs>>())
  .Callback<EventHandler<GetRightsCompletedEventArgs>>(
    callback => callback(mock.Object, new GetRightsCompletedEventArgs())
  );

When the code under test calls GetRights() on the IGetRightsProxy mock the actual EventHandler<GetRightsCompletedEventArgs> it passes in will subsequently be passed into Moq s Callback() method.

Note: Type inference will work on the generic applied to Callback() but I find in these cases it is a bit more readable to explicitly define the type being passed into the method.





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签