我希望在我的代码中对特定字典运行单元测试,试图获得一个我不希望出现在数据库中的值(在这种情况下,key=1)。
我写了以下代码:
Try
Dim s As String = myDict(1)
Catch ex As KeyNotFoundException
Assert.AreEqual("The given key was not present in the dictionary.", ex.Message)
Catch ex As Exception
Assert.Fail()
Throw
End Try
这工作正常,但代码分析抱怨“Dim s as String”声明,因为它说 s 永远不会用于任何事情。那是故意的,因为我打算为此抛出异常,而 s 是无关紧要的。
但是,我似乎找不到从代码中消除 s 的方法。只需删除分配:
Try
myDict(1)
Catch ex As KeyNotFoundException
Assert.AreEqual("The given key was not present in the dictionary.", ex.Message)
Catch ex As Exception
Assert.Fail()
Throw
End Try
现在无法编译。关于如何做到这一点的任何建议?