调用命令接口时,异步测试场景失败

时间:2017-12-28 14:14:25

标签: c# asynchronous testing xamarin icommand

我在Xamarin项目中测试ICommand场景时遇到问题。我已将逻辑提取到下面的演示中。

场景N1运行顺利但我需要场景N2才能运行。方案N2的问题是它一旦到达

await Task.Run(() => Task.Delay(1000)); 

它跳回到测试方法Assert,显然SetSurveyContext(int x)尚未执行。

最奇怪的是,如果我从应用程序内部的Xamarin框架运行此代码,一切正常,可能是因为我以错误的方式执行命令。

真的坚持这个问题,我已经尝试了很多方法来运行命令,但都没有奏效。如果有人遇到同样的问题,请帮忙。感谢。

场景1 - 工作

[Test]
public async Task NewSurvey_SendObjectWithOnlyDate_StaticSurveyResourceIdAndDateSet()
{
        var mvmTest = new TesterPage();
        await mvmTest.NewSurvey();
        Assert.That(mvmTest.setter, Is.EqualTo(3));
}

public partial class TesterPage : ContentPage
{
    public int setter = 0;

    public TesterPage()
    {
        InitializeComponent ();
    }

    public async Task NewSurvey()
    {
        await PostNewSurvey();
    }

    private async Task PostNewSurvey()
    {
        var response = await Another();
        SetSurveyContext(response);
    }

    private async Task<int> Another()
    {
       await Task.Run(() => Task.Delay(1000));
       return 3;
    }

    private void SetSurveyContext(int x)
    {
        setter = x;
    }
}

测试绿色,一切顺利。

场景N2 - 失败

[Test]
public async Task NewSurvey_SendObjectWithOnlyDate_StaticSurveyResourceIdAndDateSet()
{
        var mvmTest = new TesterPage();
        mvmTest.NewSurveyCommand.Execute(null);
        Assert.That(mvmTest.setter, Is.EqualTo(3));
}

public partial class TesterPage : ContentPage
{
    public int setter = 0;

    public TesterPage ()
    {
        InitializeComponent ();
        NewSurveyCommand = new Command(async () => await NewSurvey());
    }

    public ICommand NewSurveyCommand { get; private set; }

    public async Task NewSurvey()
    {
        await PostNewSurvey();
    }

    private async Task PostNewSurvey()
    {
        var response = await Another();
        SetSurveyContext(response);
    }

    private async Task<int> Another()
    {
       await Task.Run(() => Task.Delay(1000));
       return 3;
    }

    private void SetSurveyContext(int x)
    {
        setter = x;
    }
}

1 个答案:

答案 0 :(得分:1)

因为我在聊天中解决了@YuriZolotarev的这个问题,所以我们找到了遇到它的其他人的解决方案:
问题:
Task.Run()中调用TesterPage.Another()时,主线程会跳回到测试方法。即使在Assert.That()SetSurveyContext设置为setter之前,它也会立即执行3解决方案:
我们找到了在TesterPage中创建新私有字段的解决方案,该字段应包含Task中开始的TesterPage.Another()。可以通过Reflection在测试方法中等待此Task。一切都看起来像这样:

[Test]
public async Task NewSurvey_SendObjectWithOnlyDate_StaticSurveyResourceIdAndDateSet()
{
    var mvmTest = new TesterPage();
    mvmTest.NewSurveyCommand.Execute(null);
    // Use Reflection to wait for the task
    (GetInstanceField(typeof(TesterPage), mvmTest, "runningTask") as Task).Wait();
    Assert.That(mvmTest.setter, Is.EqualTo(3));
}

// A helper method to simplify Reflection
internal static object GetInstanceField(Type type, object instance, string fieldName)
{
    BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
    | BindingFlags.Static;
    FieldInfo field = type.GetField(fieldName, bindFlags);
    return field.GetValue(instance);
}

public partial class TesterPage : ContentPage
{
    public int setter = 0;
    private Task runningTask; // The field our Task object is saved in

    public TesterPage ()
    {
        InitializeComponent ();
        NewSurveyCommand = new Command(async () => await (runningTask = NewSurvey()));
    }

    public ICommand NewSurveyCommand { get; private set; }

    public async Task NewSurvey()
    {
        await PostNewSurvey();
    }

    private async Task PostNewSurvey()
    {
        var response = await Another();
        SetSurveyContext(response);
    }

    private async Task<int> Another()
    {
        await Task.Run(() => Task.Delay(1000));
        return 3;
    }

    private void SetSurveyContext(int x)
    {
        setter = x;
    }
}