Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2008

Tip: Looking for answers? Try searching our database.

How to use VS Unit Test where code uses a BackgroundWorker

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew Bingham - 04 Mar 2008 11:34 GMT
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)
Cowboy (Gregory A. Beamer) - 04 Mar 2008 15:01 GMT
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.

Signature

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

*************************************************

| Think outside the box!

*************************************************
>A feature I want to test calls a BackgroundWorker.
> How do I use Visual Studio Unit Test to do this?
[quoted text clipped - 22 lines]
> do some other UI task. So this seems a common-ish thing to want to do and
> test)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.