How to use VS Unit Test where code uses a BackgroundWorker

  • Thread starter Thread starter Andrew Bingham
  • Start date Start date
A

Andrew Bingham

A feature I want to test calls a BackgroundWorker.
How do I use Visual Studio Unit Test to do this?

I can see why the callback method DoWork_Completed (in the sample below) is
NOT reached during when running Test1. Is there a way of achieving this?

<TestMethod()> Public Sub Test1()

Dim txn As MyTransaction = New myTransaction
txn.DoWork(AddressOf Me.DoWork_Completed, someParameters)

End Sub

Private Sub DoWork_Completed(ByVal results as DataSet)

Assert.AreEqual(results.SourceInfo.Rows(0).Item(0),"X")

End Sub

(BTW My UI application invokes a BackgroundWorker when making a call to a
SQL Server stored procedure to get data for a user to view. Using a
BackgroundWorker allows the user to, for example, cancel viewing the data and
do some other UI task. So this seems a common-ish thing to want to do and
test)
 
I know of no way to test a background worker. It might be possible to inject
to test the code that calls the background worker, but I am not sure you can
test the worker itself.

This does not mean all is lost. If you encapsulate all of the code from the
background worker in another class, you can test all of the methods of that
class. While you cannot test the background worker itself, you can be pretty
safe knowing that the actual calling of the background worker will work. In
this exercie, you end up reducing your exposure tremendously.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
Back
Top