开发者

Help Writing an Asynchronous Unit Test In Silverlight

开发者 https://www.devze.com 2023-04-06 14:34 出处:网络
I am trying to write an Asynchronous Silverlight Unit Test, as mentioned in http://developer.yahoo.com/dotnet/silverlight/2.0/unittest.html. Perhaps I am struggling with lambda expressions, I am not s

I am trying to write an Asynchronous Silverlight Unit Test, as mentioned in http://developer.yahoo.com/dotnet/silverlight/2.0/unittest.html. Perhaps I am struggling with lambda expressions, I am not sure, but I am unclear how to write my named callbacks so that the asynch test completes without exception. Currently it throws an System.UnauthorizedAccessException (Invalid cross-thread access) in the call the TestComplete(), which I am guessing is because it doesn't know it's still in the testNullInsert() test?

Question - how do I write the test correctly, and if I need lambda expressions, please explain what does what please.

Below is my code so far:

[TestClass]
public class IdentityEditDatabaseTest : WorkItemTest
{
  [TestMethod, Asynchronous] public void testNullInsert()
  {
    wipeTestData(testNullInsertContinue1);
  }
  private void testNullInsertContinue1(String errorString)
  {
    IdentityProperties properties = new IdentityProperties(getContext());
    properties.setUserName(DATABASE_TEST);
    postUserEdit(properties, testNullInsertContinue2);
  }
  private void testNullInsertContinue2(String errorString)
  {
    Assert.assertTrue(errorString == null);

    wipeTestData(testNullInsertContinue3);
  }
  private void testNullInsertContinue3(String errorString)
  {
    TestComplete();
  }
}

Thanks!

EDIT: Correct code is below, thanks to @ajmccall's link!

[TestClass]
public class IdentityEditDatabaseTest : DatabaseTestCase
{
  [TestMethod, Asynchronous] public void testN开发者_开发技巧ullInsert()// throws Throwable
  {
    EnqueueCallback(() => wipeTestData(errorString1 => {

    IdentityProperties properties = new IdentityProperties(getContext());
    properties.setUserName(DATABASE_TEST);
    EnqueueCallback(() => postUserEdit(properties, errorString2 => {

    Assert.assertTrue(errorString2 == null);

    EnqueueCallback(() => wipeTestData(errorString3 => {

    EnqueueTestComplete();
  }));
  }));
  }));
  }


I think that @Anthony is right in suggesting that TestComplete() makes some call to the UI thread, and since it's called from a background thread (because it's a callback), it causes this exception to be thrown.

From reading the docs now, it says of the [Asynchronous] tag

The test has completed only after the TestComplete method is called, not when the method exits. Your test class must inherit from SilverlightTest to perform asynchronous tests

You could try putting the line TestComplete() at the end of your testNullInsert method. This may not throw the exception, but I don't think it'll be performing the test properly. What you ultimately want to do is have one test method which does the following steps,

  • Clear the test data and wait until it finishes - this is not the test, so are not concerned about the results but we still have to wait using a AutoResetEvent or continuing inside the callback
  • Define the callback that you want to test
  • Add the callback to the unit test queue - using EnqueueCallback()
  • Wait for the callback to finish and store result - using EnqueueConditional()
  • Inform the unti test framework you've finish - by calling EnqueueTestComplete()

If you can rewrite your test into something that looks like this, then I think you'll be on your way to writing asynchronous unit tests for your code.

Cheers, Al.


One of the things that will happen on TestComplete is the UI will be updated. However evidently the TestComplete method (or the UI code that it eventually interacts with) is not expecting to be called on a non-UI thread.

Hence it would seem its up to you to ensure the call to TestComplete is executed on the UI thread:-

  private void testNullInsertContinue3(String errorString)
  {
       Deployment.Current.Dispatcher.BeginInvoke(TestComplete); 
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消