WPToolkitTestFx:assertfailed时获取未处理的异常

时间:2013-03-04 12:30:40

标签: unit-testing windows-phone-8

我正在为Windows Phone 8应用程序使用WPToolkitTestFx Unit测试框架。执行Assert.Fail()Assert.IsFalse(true)时,我收到的错误低于错误。

  

Microsoft.VisualStudio.QualityTools.UnitTesting.Phone.DLL

中出现'Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException'类型的第一次机会异常

上述错误的任何解决方案。

这里是源代码

    [TestMethod]
    [Asynchronous]
    [Description("Test2: Sample asynchronous test")]
    public void Test2()
    {
        // this test executes asynchronously
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            // ... and then fails

            Assert.IsFalse(true);

            EnqueueTestComplete();
        });
    }

谢谢, 肉酱

2 个答案:

答案 0 :(得分:0)

在关注http://blogs.windows.com/windows_phone/b/wpdev/archive/2012/11/20/windows-phone-toolkit-overview.aspx的示例代码时,我可以看到类似的内容。

异常发生是正常的,请参阅:

A first chance exception of type 'Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException' occurred in Microsoft.VisualStudio.QualityTools.UnitTesting.Phone.DLL

此外,如果调试器连接到您的设备/仿真器,调试器将会中断,以便您可以找到测试失败的位置。

    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        if (Debugger.IsAttached)
        {
            // An unhandled exception has occurred; break into the debugger
            Debugger.Break(); // <- !!! application will stop in debugger here !!!
        }
    }

但是,如果继续项目(按F5),或者未在调试器下运行,您将看到应用程序继续运行并且不会退出。

这样可以让您查看测试结果。

答案 1 :(得分:-1)

问题,如果你的测试失败,这是你的测试的正常部分吗?如果是这样,您需要指示框架期望发生此类异常

[TestMethod, Asynchronous]
[Description("Test2: Sample asynchronous test")]
[ExpectedException(typeof(AssertFailedException))]
public void Test2()
{
    // this test executes asynchronously
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        // ... and then fails

        Assert.IsFalse(true);

        //TestComplete();
    });
}

另外我注意到你将方法标记为异步,但是你没有使用EnqueueDelay(TimeSpan), EnqueueCallback(), EnqueueTestComplete()的异步调用方法使方法函数异步。