0

I am creating a c# application that has an embedded webbrowser control. From javascript I am calling a c# method and passing a javascript callback. I am using the dynamic technique from this answer:

public void CallbackTest(object callback)
{
    dynamic callbackFunc = callback;
    callbackFunc("Hello!");
}

My problem is how would I go about unit testing this method? How do I mock (or fake) the callback parameter? For what it's worth, my mocking library of choice is Moq.

Community
  • 1
  • 1
arch-imp
  • 217
  • 3
  • 12

1 Answers1

1

I would imagine that you could just pass in an Action<String> and this should work as a callback. I am not sure if you can get support for this kind of dynamic in Moq directly, though. But, using an Action is pretty straightforward anyway

var mockCallBack = new Action<String>(str => {return;});
CallbackTest(mockCallBack)
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180