Urk*_*Urk 13 c# syntax gated-checkin visual-studio-2017
有人可以帮忙赐教吗?
我去办理了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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,请注意"case PaymentStatus Unknown"行缺少'.' 点运算符.如上所述,该项目建立并运行; 但未能使用门控构建服务器签入.
另请注意,以下测试正在通过:
[TestMethod]
public void StatusStringTest_Unknown()
{
var model = new PaymentViewModel()
{
Status = PaymentStatus.Unknown
}
Assert.AreEqual("Unknown", model.StatusString);
}
Run Code Online (Sandbox Code Playgroud)
最后,请注意我只使用静态字符串运行测试,而不是使用资源文件,然后通过.为了简单起见,我在上面的代码中省略了资源文件的内容.
对此有任何想法非常感谢!提前致谢!
das*_*ght 11
这是因为您的Visual Studio解释PaymentStatus Unknown为模式匹配,这是C#7 的新功能:
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!";
}
Run Code Online (Sandbox Code Playgroud)
如果TFS设置为使用旧版本的C#,它将拒绝此来源.
注意:您的单元测试工作的原因是剩余的情况都正确完成.InavlidEnumerationException(this.Status)但是,抛出的测试用例会失败,因为开关会将任何未知值解释为PaymentStatus.Unknown.
| 归档时间: |
|
| 查看次数: |
254 次 |
| 最近记录: |