Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

Tags: nunit

The Desktop Developers Journal

Nunit: StatThread and WindowsForms Controls

by chrissie1


Permalink 11 Sep 2008 02:40 , Categories: Microsoft Technologies Tags: .net, nunit, statthread, windows forms

When you try to instantiate a windowsforms control in your NUnit test, you could get the following error:

System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

This has something to do with the fact that windows forms controls like to run in STA (Single threaded apartment) and the latests version of Nunit run as MTA (Multi Threaded apartment).

After some googling (which only returned a small number of hits), I found “Nunit and STATThread by frater”. The solution happens to be very simple. Add this to the assembly where you have the test and the error will go away.

  1. <configSections>
  2.     <sectionGroup name="NUnit">
  3.       <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
  4.     </sectionGroup>
  5.   </configSections>
  6.  
  7.   <NUnit>
  8.     <TestRunner>
  9.       <add key="ApartmentState" value="STA" />
  10.     </TestRunner>
  11.   </NUnit>

So another day and another problem solved.

1 comment »Send a trackback » 383 views

VB.Net: Check if an Event Gets Raised By a Method

by chrissie1


Permalink 29 Aug 2008 06:09 , Categories: Microsoft Technologies Tags: nunit, rhino mocks, unittest, vb.net

Let’s say we want to test to see if our object really throws that event when we need it to.

  1. Public Class EventThrower
  2.   Public Event throwme(byval somestring as String)
  3.  
  4.   Public Sub SomeSub()
  5.     RaiseEvent throwme("Yeepie!")
  6.   End Sub
  7. End Class

So when calling a method SomeSub an event throwme will be raised.

Now we make a little unittest class that looks like this.

  1. Imports NUnit.Framework
  2. Imports Rhino.Mocks
  3.  
  4. <TestFixture()> _
  5. Public Class TestEvent
  6.  
  7. #Region " Private members "
  8.         Private _Mocker As MockRepository
  9. #End Region
  10.  
  11. #Region " Setup and TearDown "
  12.         <Setup()> _
  13.         Public Sub Setup()
  14.             _Mocker = New MockRepository
  15.         End Sub
  16.  
  17.         <TearDown()> _
  18.         Public Sub TearDown()
  19.             _Mocker.ReplayAll()
  20.             _Mocker.VerifyAll()
  21.         End Sub
  22. #End Region
  23.  
  24. #Region " Tests "
  25.         <Test()> _
  26.         Public Sub Test_NewFile_If_RaisesEvent_NewFileMade()
  27.             Dim subscriber As ISubscriber = _Mocker.DynamicMock(Of ISubscriber)()
  28.             Dim _EventThrower As New EventThrower
  29.             AddHandler _EventThrower.throwme, AddressOf subscriber.throwmesub
  30.             subscriber.throwmesub(Nothing)
  31.             LastCall.IgnoreArguments()
  32.             _Mocker.ReplayAll()
  33.             _EventThrower.SomeSub()
  34.         End Sub
  35. #End Region
  36.  
  37.  
  38.         Public Interface ISubscriber
  39.             Sub throwmesub(ByVal somestring As String)
  40.         End Interface
  41.  
  42.     End Class
  43.  
  44.    
  45. End Namespace

Just a few notes. I created an interface just for this test. And interface with a method with the same signature as the event. Then in the Test, I subscribe the event to that method. I didn’t forget to set IgnoreArguments and I called the Method on our object.

If the test goes green, we know that the event is raised when we call the sub. Now it’s just a matter of subscribing to that event.

Leave a comment »Send a trackback » 265 views