Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one test async code using MSTest

I'm writing some super simple async code. Just saving a file off-thread.

I'd like to test this code using the MSTest unit test framework in Microsoft Visual Studio Team System 2008.

How do I do this?

I'd like to simple block the test method until the method returns. I can imagine some ways to do this, but I'm blown away there aren't any best practices or helper classes around this.

I see a lot for Silverlight, but nothing generic.

like image 438
Kevin Moore Avatar asked Jan 13 '10 22:01

Kevin Moore


People also ask

How do you test asynchronous?

To test asynchronous code, we use the XCTestExpectation class and wait for the expected outcome. The workflow is to create an expectation, and then when the asynchronous task completes successfully, we fulfil that expectation. We will wait for a specific amount of time for the expectation to be fulfilled.

How do I run a MSTest unit test?

To run MSTest unit tests, specify the full path to the MSTest executable (mstest.exe) in the Unit Testing Options dialog. To call this dialog directly from the editor, right-click somewhere in the editor and then click Options.

How do you test async methods Jasmine?

If an operation is asynchronous just because it relies on setTimeout or other time-based behavior, a good way to test it is to use Jasmine's mock clock to make it run synchronously. This type of test can be easier to write and will run faster than an asynchronous test that actually waits for time to pass.


1 Answers

Visual studio 2012 (previously known as "Visual Studio 11") introduced support for async tests. It looks like this:

[TestMethod] public async Task FooTest() {    var result = await SomeAsyncOperation();    Assert.IsTrue(someCondition); } 

As noted in the comments, the Task return type is important. It won't work if you declare the method as returning void.

like image 52
Wim Coenen Avatar answered Sep 28 '22 08:09

Wim Coenen