【问题标题】:Ensuring Service Connectivity确保服务连接
【发布时间】:2023-03-17 00:40:01
【问题描述】:

我打算设置一个非常简单的 NUnit 测试来测试 WCF 服务是否启动并运行。 所以,我有

http://abc-efg/xyz.svc

现在,我需要编写一个单元测试来连接到这个 URI,如果它正常工作,只需记录成功,如果失败,则在文件中记录失败和异常/错误。没有必要单独托管等。

调用和实现这一目标的理想方法和方法是什么?

【问题讨论】:

    标签: c# wcf unit-testing nunit connectivity


    【解决方案1】:

    这是我们在连接到 WCF 服务器的测试中使用的。我们没有明确测试服务器是否启动,但显然如果不是,那么我们会收到错误:

    [Test]
    public void TestServerIsUp()
    {
        var factory = new ChannelFactory<IMyServiceInterface> (configSectionName);
        factory.Open ();
        return factory.CreateChannel ();
    }
    

    如果在配置中指定的端点上没有端点监听,那么你会得到一个异常和一个失败的测试。

    如果需要,您可以使用 ChannelFactory 构造函数的其他重载之一来传递固定的绑定和端点地址,而不是使用配置。

    【讨论】:

      【解决方案2】:

      不确定这是否理想,但如果我理解您的问题,您确实在寻找集成测试以确保某个 URI 可用。您并不是真的要对服务的实现进行单元测试——您想向 URI 发出请求并检查响应。

      这是我设置用来运行它的 NUnit TestFixture。请注意,这很快就完成了,绝对可以改进......

      我使用 WebRequest 对象发出请求并返回响应。发出请求时,它被包裹在 try...catch 中,因为如果请求返回的响应不是 200 类型的响应,它将抛出 WebException。所以我捕获了异常并从异常的Response 属性中获取WebResponse 对象。我在那时设置了我的 StatusCode 变量并继续评估返回的值。

      希望这会有所帮助。如果我误解了您的问题,请告诉我,我会相应地更新。祝你好运!

      测试代码:

      [TestFixture]
      public class WebRequestTests : AssertionHelper
      {
          [TestCase("http://www.cnn.com", 200)]
          [TestCase("http://www.foobar.com", 403)]
          [TestCase("http://www.cnn.com/xyz.htm", 404)]
          public void when_i_request_a_url_i_should_get_the_appropriate_response_statuscode_returned(string url, int expectedStatusCode)
          {
              var webReq = (HttpWebRequest)WebRequest.Create(url);
              webReq.Method = "GET";
              HttpWebResponse webResp;
              try
              {
                  webResp = (HttpWebResponse)webReq.GetResponse();
      
                  //log a success in a file
              }
              catch (WebException wexc)
              {
                  webResp = (HttpWebResponse)wexc.Response;
      
                  //log the wexc.Status and other properties that you want in a file
              }
      
              HttpStatusCode statusCode = webResp.StatusCode;
              var answer = webResp.GetResponseStream();
              var result = string.Empty;
      
              if (answer != null)
              {
                  using (var tempStream = new StreamReader(answer))
                  {
                      result = tempStream.ReadToEnd();
                  }
              }
      
              Expect(result.Length, Is.GreaterThan(0), "result was empty");
              Expect(((int)statusCode), Is.EqualTo(expectedStatusCode), "status code not correct");
          }
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用 Visual Studio 中的单元测试功能来执行此操作。这是一个例子

        http://blog.gfader.com/2010/08/how-to-unit-test-wcf-service.html

        【讨论】:

          【解决方案4】:

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-18
            • 2018-04-04
            • 1970-01-01
            • 1970-01-01
            • 2012-11-29
            • 2020-06-08
            相关资源
            最近更新 更多