1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[VB.NET][Visual Studio 2015]asyncが付いている非同期メソッドをテストする

Posted at

asyncが付いている非同期メソッドをテストする話です。

環境

  • Visual Studio 2015 Community Update 2
  • VB.NET

前提

例えば、こんな感じに定義された非同期メソッドがあったとします。

Foo.vb
Public Class Foo
    Public Async Shared Function BarAsync() As Task
      ' ...
    End Function
End Class

動かないテスト

そこでこのようなテストを書きました。

FooTest.vb
<TestClass()>
Public Class FooTest
    <TestMethod()>
    Public Sub TestBarAsync()
        Foo.BarAsync()
    End Sub
End Class

が、何が起こってもテストは成功したと報告してきます。
asyncのせいです。

実行されないテスト

そこで非同期メソッド呼び出し部分にAwaitを付けると、テストが実行されません。

FooTest.vb
<TestClass()>
Public Class FooTest
    <TestMethod()>
    Public Async Sub TestBarAsync()
        Await Foo.BarAsync()
    End Sub
End Class

よく見ると、こんなメッセージが出ています。

Method TestBarAsync defined in class FooTest does not have correct signature. Test method marked with the [TestMethod] attribute must be non-static, public, return-type as void and should not take any parameter. Example: public void Test.Class1.Test(). Additionally, return-type must be Task if you are running async unit tests. Example: public async Task Test.Class1.Test2()

テストメソッドをasyncにしたときは、Taskを返さないといけないです。

Taskを返す

テストメソッドをSubからFunctionにして、戻り値をTaskにしたところ動きました。

FooTest.vb
<TestClass()>
Public Class FooTest
    <TestMethod()>
    Public Async Function TestBarAsync() As Task
        Await Foo.BarAsync()
    End Sub
End Class

参考リンク

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?