How to mock delegates in moq

I have a class implementation as shown below. MethodUnderTest () is a method that calls the delegate to update the grid using some custom filter with a callback function - UpdateGridCallback.

public class MyClass
{
    public delegate void UpdateGridDelegate(MyCustomFilters filter);
    public UpdateGridDelegate del;
    public MyCustomFilters filter;

    public void MethodUnderTest()
    {
        //.... some code...
        // For simplicity of example I am passing somename.. should be passing rows..

        // set status bar saying retriving data..

       del = new UpdateGridDelegate(UpdateGrid);
       del.BeginInvoke(filter, UpdateGridCallback, null);
    }

    public void UpdateGrid(MyCustomFilters filter)
    {
       // Upadte Grid with passed rows.
    }

    public void UpdateGridCallback(IAsyncResult result)
    {
       // callback .. do some action here.. like updating status bar saying - Ready
    }
}

I use Nunit and Moq.delegate. MethodUnderTest () is the method that is being tested. How do I make fun of the delegate that is used in MethodUnderTest ()?

I want to make sure that the delegate was called (or a callback was made) from my NUnit test case.

+4
source share
1 answer

, new MethodUnderTest. , , del .

, . del , :

public class MyClass
{
    public delegate void UpdateGridDelegate(MyCustomFilters filter);
    public UpdateGridDelegate del;
    public MyCustomFilters filter;

    public MyClass(UpdateGridDelegate del)
    {
        this.del = del;
    }

    // etc.
}

.

+3

Source: https://habr.com/ru/post/1533497/


All Articles