简体   繁体   English

连接的单元测试/ C#

[英]Unit test for connection / C#

Writing test for my application. 为我的应用程序编写测试。 Would like to test connection with exeption handling, for now I have created method which works and looks like : 想测试与exeption处理的连接,目前,我创建了可以正常工作的方法,如下所示:

        [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");
        }

In the finall version working on smth which will catch exception - WebExeption . 在处理smth的最终版本中,它将捕获异常WebExeption Already have it in try / catch block inside my method which is about to create connection, it works ofc. 已经在要创建连接的方法中的try / catch块中添加了它,它可以正常工作。 But need it in my test as well. 但是在我的测试中也需要它。 I was thinking it should looks like : 我以为它应该看起来像:

[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"););
        }

Like we can see i changed first arg of the method which is URL address, it will couse the web exeption. 就像我们看到的那样,我更改了方法的第一个参数(即URL地址),它将导致网络异常。 How can i write it in correct way ? 我该如何正确书写?

I don't think there is anything wrong in your approach of testing the exception. 我认为您测试异常的方法没有任何问题。 I do suggest, however, that you split your test into two tests - one for the case where you get a valid connection, and one for the case where you get a bad connection. 但是,我确实建议您将测试分为两个测试-一个用于获得有效连接的测试,另一个用于获得不良连接的测试。

    [Test]
    public void WhenCorrectUrlIsPassedConnectionCreatedSuccessfully()
    {
        Connection testConnection = new Connection();
        connection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name");
    }

    [Test]
    [ExpectedException(typeof(WebException))]
    public void WhenIncorrectUrlIsPassedThenWebExceptionIsThrown()
    {
        Connection connection = new Connection();
        Assert.Catch<WebException>(() => connection.CreateConnection("test", IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name"););
    }

This is without knowing the exact details of how you implemented the test connection. 这是不知道有关如何实现测试连接的确切详细信息。 If you have some internal component in charge of creating the connection, and your code is a wrapper around it, then you should consider passing the internal component in as an Interface and mocking its behavior. 如果您有负责创建连接的内部组件,而您的代码是围绕它的包装,则应考虑将内部组件作为接口传入并模拟其行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM