Hi folks,

While trying to build a test suite using NUnit, I ran into a strange
problem involving delegates (it might be easier to read the code than go
through the explanation).

The scenario is extremely simple -- the test method, TestMe(), creates a
instance of another class called TestDelegate, which declares a delegate
called CallBackDelegate. Inside the constructor of TestDelegate, a new
instance of CallBackDelegate is created using a member function called
DoSomething().

However, when the test case is invoked, it fails in BeginInvoke() because
the type TestDelegate() cannot be loaded!!! Obviously, everything works
just fine if the code is not invoked from NUnit.

To test, compile TestDelegate.cs into a DLL assembly. Compile TestMe.cs
into another DLL assembly, add a reference to TestDelegate.dll,
Nunitcore.dll, etc...

[TestDelegate.cs]
using System;

namespace CallMe
{
 public class TestDelegate
 {
  delegate void CallBackFunction();

  public TestDelegate()
  {
   new CallBackFunction(DoSomething).BeginInvoke
(null,null);
  }

  public void DoSomething()
  {
   Console.WriteLine("Delegate called...");
  }
 }
}

[Testme.cs]

using System;
using System.Collections;
using NUnit.Extensions;
using NUnit.Framework;

namespace Test
{
 public class MyTest : TestCase
 {
  public MyTest() {}
  public MyTest(string Foo) : base(Foo){}
  public void TestMe()
  {
   new CallMe.TestDelegate();
   System.Diagnostics.Debug.Assert(false);
  }

  public static ITest Suite
  {
                        get
   {
    TestSuite suite= new TestSuite();
    suite.AddTest(new MyTest("TestMe"));
    return suite;
   }
  }
 }
}


Atul

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to