0
0

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 1 year has passed since last update.

C# 非同期メソッドをテスト用にmock化したい

Posted at

目的

非同期メソッドをテスト用にmock化したい

こうやって見た

Task を作る部分をmock化するとやりやすい
Task.FromResult で作ったものを async なメソッドでつかってやればよい

using System;
using System.Threading.Tasks;

public class Hello
{
    public static void Main()
    {
        var test = new TestClass();
        Console.WriteLine(test.GetInt().Result);
    }
}

public abstract class AbClass
{
    public abstract Task<int> MakeTask();

    public async Task<int> GetInt()
    {
        return await MakeTask().ConfigureAwait(false);
    }
}

public class TestClass : AbClass
{
    public override Task<int> MakeTask()
    {
        return Task.FromResult(100);
    }
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?