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