为我的应用程序编写测试。想测试与异常处理的连接,现在我已经创建了有效的方法,看起来像:
[Test]
public void TestCreateConnection()
{
Connection testConnection = new Connection();
connection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name");
testConnection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name");
}
在最终版本的 smth 中,它将捕获异常 - WebExeption
。在我即将创建连接的方法中的 try / catch 块中已经有了它,它可以正常工作。但在我的测试中也需要它。我在想它应该看起来像:
[Test]
[ExpectedException(typeof(WebException))]
public void TestCreateConnection()
{
Connection testConnection = new Connection();
connection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name");
testCconnection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name");
Assert.Catch<WebException>(() => connection.CreateConnection("test", IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name"););
}
就像我们可以看到我改变了方法的第一个参数是 URL 地址,它会导致 web exeption。我怎样才能以正确的方式编写它?