I am trying to run a unit test using the following VS test method.
void Get(string name, Action<string> callBack);
here is the device 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 the internal implementation uses BackgroundWorker, so the callback is called on another thread. Here is the 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) {
Of course, since Get () returns immediately, the test succeeds and testing stops, so RunWorkerCompleted never starts. I can easily test this with a regular application (WPF) because it works, but I would like to be able to unit test this.
Any ideas? Thanks in advance.
source share