Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing closures

I have a very simple method I'm trying to test. _interactionService puts up dialog that asks the user for confirmation about deleting the customer. If the appropriate button is clicked on the box the supplied action is called. The action manipulates the context and saves changes. After the save is complete another action is called.

    private void Delete(object entity)
{
    _interactionService.ShowConfirmationBox("Delete?", "Are you sure you want to delete this customer?", () =>
        {
            Customer customer = entity as Customer;
            Context.Attach(customer);
            Context.Delete(customer);
            Context.Save(() => DoSomethingElseWhenSaveComplete);
        });
}

I'm at a loss as to how to test this method. I have mocked the service and the context, but how to test the closures?

like image 727
foo Avatar asked May 15 '26 21:05

foo


1 Answers

If the context is mocked, then you can use a callback in your test:

Action saveAction = null;
contextMock
  .Setup(c => c.Save(It.IsAny<Action>())
  .Callback<Action>(a => saveAction = a);

// Call delete...

Assert.IsNotNull(saveAction);

saveAction();

// Assert that DoSomethingElseWhenSaveCompleted was done
like image 183
Rich O'Kelly Avatar answered May 18 '26 12:05

Rich O'Kelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!