public class UnitTest1
{
// you cant use a brush because it is UI
// you switch to a UI test and it fails because it is async
// bottom line you cant test an async routine that uses a brush,
// even though your really are not doing ANY UI stuff - how stupid is that?
[TestMethod]
//[Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer.UITestMethod]
async public Task TestMethod1()
{
var t = new ObservableCollection<CommentEvent>();
t.Add(new CommentEvent(Colors.LightPink) { Label = "Bad",
EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
TimeCode.SmpteFrameRate.Smpte2997Drop) });
t.Add(new CommentEvent(Colors.DarkSeaGreen) { Label = "Good",
EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
TimeCode.SmpteFrameRate.Smpte2997Drop) });
t.Add(new CommentEvent(Colors.LightPink) { Label = "Bad",
EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
TimeCode.SmpteFrameRate.Smpte2997Drop) });
t.Add(new CommentEvent(Colors.DarkSeaGreen) { Label = "Good",
EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
TimeCode.SmpteFrameRate.Smpte2997Drop) });
var s = await PreludeXMP.Get(t);
Assert.IsNotNull(s);
System.Diagnostics.Debug.WriteLine(s);
}
}
这会抛出一个
该应用程序调用了一个为a编组的接口 不同的线程。 (HRESULT的例外情况:0x8001010E (RPC_E_WRONG_THREAD))。如果您在测试中使用UI对象考虑 使用[UITestMethod]属性而不是[TestMethod]来执行测试 在UI线程中。
因为CommentEvent的构造函数创建了一个Solid.Brush。任何建议的工作方式?
答案 0 :(得分:0)
在我的一个测试中尝试使用WriteableBitmap和异步方法时遇到了一个非常类似的问题。在搜索使其成功的方式时,我偶然发现了您的问题并chue x's answer提到了评论。
使用他的方法,我设法使其即使使用异步调用也能正常工作。在您的情况下,测试代码可以像这样修改:
[TestMethod]
async public Task TestMethod1()
{
var taskSource = new TaskCompletionSource<object>();
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal, async () =>
{
try
{
var t = new ObservableCollection<CommentEvent>();
t.Add(new CommentEvent(Colors.LightPink) { Label = "Bad",
EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
TimeCode.SmpteFrameRate.Smpte2997Drop) });
t.Add(new CommentEvent(Colors.DarkSeaGreen) { Label = "Good",
EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
TimeCode.SmpteFrameRate.Smpte2997Drop) });
t.Add(new CommentEvent(Colors.LightPink) { Label = "Bad",
EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
TimeCode.SmpteFrameRate.Smpte2997Drop) });
t.Add(new CommentEvent(Colors.DarkSeaGreen) { Label = "Good",
EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
TimeCode.SmpteFrameRate.Smpte2997Drop) });
var s = await PreludeXMP.Get(t);
Assert.IsNotNull(s);
System.Diagnostics.Debug.WriteLine(s);
askSource.SetResult(null);
}
catch (Exception e)
{
taskSource.SetException(e);
}
}
await taskSource.Task;
}
我已经在我刚刚发布的a blog post中用自己的例子写了一个更深入的解释。