I'm trying to unit test, using VS unit testing facility following method.
void Get(string name, Action<string> callBack);
here is unit tester
[TestMethod]
public void Test()
{
Action<string> cb = name =>
{
Assert.IsNotNull(name);
};
var d = new MyClass();
d.Get("test", cb);
}
The only problem is that internal implementation uses BackgroundWorker, hence callback is invoked on another thread. Here is internal implementation.
public void Get(string name, Action<string> callBack)
{
callBackString = callBack;
GetData(name);
}
private void GetData(string name)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
bw.RunWorkerAsync(name);
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//do work here
if (null != callBackString)
callBackString("ok");
}
Of course because Get() returns right away, test completes with success and testing stops, thus RunWorkerCompleted never gets to be executed. I can easily test this via normal application (WPF) because it stays running, yet I'd like to have ability to unit test this.
Any ideas? Thanks in advance.