首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows Phone测试失败

Windows Phone测试失败
EN

Stack Overflow用户
提问于 2012-03-27 19:42:24
回答 1查看 71关注 0票数 0

遵循this教程,遇到了麻烦。

代码语言:javascript
复制
 [TestMethod]
    [ExpectedException(typeof(Exception))]
    public void VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException()
    {
        var customer = new Customer() { FirstName = "June", LastName = "Smith" };
        var sut = new CustomerViewModel(_customerRepository, customer);
        sut.VerifyPropertyName("NonExistentPropertyName");

    }

测试失败,并显示以下消息。测试显然抛出了一个异常,但它应该抛出异常!为什么测试会失败呢?

代码语言:javascript
复制
 VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException : FailedTest method FirstOnSiteWindowsPhoneApp.AppCode.Tests.Unit.CustomerViewModelTests.VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException threw exception: 
FirstOnSiteWindowsPhoneApp.AppCode.Domain.VerifyPropertyNameException: Exception of type 'FirstOnSiteWindowsPhoneApp.AppCode.Domain.VerifyPropertyNameException' was thrown.
at FirstOnSiteWindowsPhoneApp.AppCode.ViewModel.CustomerViewModel.VerifyPropertyName(String propertyName) in CustomerViewModel.cs: line 29
at FirstOnSiteWindowsPhoneApp.AppCode.Tests.Unit.CustomerViewModelTests.VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException() in CustomerViewModelTests.cs: line 53
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-27 19:46:19

您预期的异常类型错误。它应该是:

代码语言:javascript
复制
[ExpectedException(typeof(VerifyPropertyNameException))]

这也是本教程所展示的,所以我不确定为什么您要使用typeof(Exception)……

ExpectedException完全需要指定类型的异常,而不仅仅是从它派生的任何类型的异常。请注意,就我个人而言,我更喜欢Assert.Throws<...>(() => ...),因为这样可以限制预期抛出的代码的范围,但这是另一回事。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9888894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档