有人可以帮助赐教吗?
我去办理了TFS的一些变更,我的办理登机手续被拒绝了。它促使我看一下我编辑过的switch语句。
我发现Visual Studio 2017声称没有编译时间问题,并允许我成功构建和部署应用程序。最重要的是,即使方法的单元测试似乎也按预期传递。
public enum PaymentStatus
{
Issued,
Cleared,
Voided,
Paid,
Requested,
Stopped,
Unknown
}
public class PaymentViewModel
{
public PaymentStatus Status { get; set; }
...
public String StatusString
{
get
{
switch (this.Status)
{
case PaymentStatus.Cleared:
return "Cleared";
case PaymentStatus.Issued:
return "Issued";
case PaymentStatus.Voided:
return "Voided";
case PaymentStatus.Paid:
return "Paid";
case PaymentStatus.Requested:
return "Requested";
case PaymentStatus.Stopped:
return "Stopped";
case PaymentStatus Unknown:
return "Unknown";
default:
throw new InavlidEnumerationException(this.Status);
}
}
}
}
所以,请注意,“案例PaymentStatus Unknown”一行缺少'。'点运算符。如上所述,该项目建立并运行;但未能使用门控构建服务器签到。
另请注意,以下测试正在通过:
[TestMethod]
public void StatusStringTest_Unknown()
{
var model = new PaymentViewModel()
{
Status = PaymentStatus.Unknown
}
Assert.AreEqual("Unknown", model.StatusString);
}
最后,请注意我只使用静态字符串运行测试,而不是使用资源文件,然后通过。为了简单起见,我在上面的代码中省略了资源文件的内容。
对此的任何想法都非常感谢!在此先感谢!
答案 0 :(得分:11)
这是编译因为您的Visual Studio将PaymentStatus Unknown
解释为模式匹配,这是C#7的new feature:
PaymentStatus
是类型,Unknown
是名称,此语法的预期用例如下:
switch (this.Status) {
case PaymentStatus ended when ended==PaymentStatus.Stopped || ended==PaymentStatus.Voided:
return "No payment for you!";
default:
return "You got lucky this time!";
}
如果TFS设置为使用较旧版本的C#,它将拒绝此来源。
注意:您的单元测试工作的原因是剩余的情况都正确完成。但是,抛出InavlidEnumerationException(this.Status)
的测试用例会失败,因为切换会将任何未知值解释为PaymentStatus.Unknown
。