Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6604309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:08:15+00:00 2026-05-25T19:08:15+00:00

I’m writing an asynch unit test and I would like to string it together

  • 0

I’m writing an asynch unit test and I would like to string it together using lambdas (or anonymous methods?) so I don’t have to define named functions for the continuations.

I have read several posts on lambdas, but most of these deal with for each style constructs which I am not interested in.

I would like to do something like the following (taken from here):

using Microsoft.Silverlight.Testing;  
using Microsoft.VisualStudio.TestTools.UnitTesting;  
{  
    [TestClass]  
    public class Test2 : SilverlightTest  
    {  
        [TestMethod]  
        [Asynchronous]  
        public void TestAsync1()  
        {  
            var eventRaised = false;  
            var result = false;  
            var timer = new System.Windows.Threading.DispatcherTimer();  

            timer.Interval = TimeSpan.FromSeconds(2);  
            timer.Tick += (object sender, EventArgs e) =>  
                {  
                    timer.Stop();  

                    // Simulate an expected result  
                    result = true;  

                    // Mark the event has being raised  
                    eventRaised = true;  
                };  

            // Start timer  
            EnqueueCallback(() => timer.Start());  

            // Wait until event is raised  
            EnqueueConditional(() => eventRaised);  
            EnqueueCallback(() =>  
            {  
                // Additional tasks can be added here  
                Assert.IsTrue(result);  
            });  

            EnqueueTestComplete();  
        }  
    }  
}  

But I guess I don’t need the EnqueueCallback() stuff.

The following is my code without lambdas:

[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();
  }
}

...

Again, the question is:

How can I string the above together using lambdas (or anonymous methods?) so I don’t have to define named functions for the continuations?

Please explain the new syntax as much as possible, as I’m still trying to wrap my head around the concept.

Many thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:08:15+00:00Added an answer on May 25, 2026 at 7:08 pm

    If we have the following method:

    private void DoSomething(object argument)
    {
        // Do something with the argument here
    }
    

    you’re probably aware that it can be assigned to a delegate variable like this:

    Action<object> asDelegate = DoSomething;
    

    to make this same assignment using an anonymous method, we can use a lambda expression:

    Action<object> asDelegate = (object argument) =>
        {
            // Do something with the argument here
        }
    

    So in your example, the method testNullInsert could be written like this:

    [TestMethod, Asynchronous]
    public void testNullInsert()
    {
        wipeTestData((string errorString) =>
        {
            IdentityProperties properties = new IdentityProperties(getContext());
            properties.setUserName(DATABASE_TEST);
            postUserEdit(properties, testNullInsertContinue2);
        });
    }
    

    All I’ve done there is replaced the name testNullInsertContinue1 with a lambda expression containing the same functionality. You could do the same thing with testNullInsertContinue2 as well if you want.

    Once you get more familiar with using lambda expressions, you can drop things like the brackets around the argument (if there’s only one argument) and the types of the arguments as the compiler can often infer them, but I’ve written it like this to try to give you as good an idea of what’s going on as possible. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.