【问题标题】:Unit testing class in a web service in .net.net 中的 Web 服务中的单元测试类
【发布时间】:2011-02-20 16:22:31
【问题描述】:

经过一番挖掘,我接受了这个帖子中的建议:How to unit test C# Web Service with Visual Studio 2008

我创建了一个单独的类,而我的 Web 服务类只是该类的包装器。问题是当我尝试在 VS2008 中创建一个单元测试项目时,它坚持创建一个单元测试,就像我正在测试 Web 服务调用而不是我指定的类一样。我无法进入我正在尝试测试的课程。

我有一个网络服务“subscription_api.asmx”。后面的代码是“subscription_api.cs”,其中包含对“subscription.cs”处真实代码的 Web 方法包装器调用。

我希望能够做到以下几点:

[TestMethod()]
        public void GetSystemStatusTest()
        {
            subscription sub = new subscription();
            XmlNode node = sub.GetSystemStatusTest();
            Assert.IsNotNull(node);
        }

但是我得到了这个从 VS'08 自动生成的混乱:

/// <summary>
        ///A test for GetSystemStatus
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\CVSROOT\\rnr\\pro\\product\\wms\\ss\\subscription_api", "/subscription_api")]
        [UrlToTest("http://localhost/subscription_api")]
        public void GetSystemStatusTest()
        {
            subscription_Accessor target = new subscription_Accessor(); // TODO: Initialize to an appropriate value
            XmlNode expected = null; // TODO: Initialize to an appropriate value
            XmlNode actual;
            actual = target.GetSystemStatus();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

此外,Test References 文件夹中有一个“subscription_api.accessor”。

当我尝试这个时:

[TestMethod()]
public void GetSystemStatusTest2()
{
    subscription_Accessor sub = new subscription_Accessor();
    XmlNode node = sub.GetSystemStatus();
    Assert.IsNotNull(node);
}

我收到一个错误:

Test method subscription_api.Test.subscriptionTest.GetSystemStatusTest2 threw exception:  System.TypeInitializationException: The type initializer for 'subscription_Accessor' threw an exception. --->  System.ArgumentNullException: Value cannot be null.

我对单元测试很陌生,感觉很迷茫。如何在不测试 Web 服务的情况下为“subscription.cs”中的订阅类创建单元测试?我是否仅限于在同一个项目中进行测试(我希望不是)?我是否必须将目标类放在 Web 服务项目之外的自己的项目中?

【问题讨论】:

    标签: .net web-services unit-testing


    【解决方案1】:

    听起来您的网络服务是在“网站”项目而不是“网络应用程序”项目中创建的。问题是网站不是构建到您可以引用的单个 DLL 上的,就像 Web 项目一样。

    如果是这种情况,并且您使用的是网站类型的项目,那么您需要将其转换为 Web 应用程序项目,以便直接利用单元测试代码。那里有很多关于如何转换项目的博客文章,只需搜索“将 asp.net 网站转换为 Web 应用程序”即可。

    或者/另外,您可能希望将此代码移动到它自己的类库中,并在包含您的 Web 服务的项目中引用该库。

    【讨论】:

    • 好的,谢谢您的信息。我忘记提到的一件事是,这个项目是从 VS'05 转换为 VS'08 的,目的是使其可测试。
    • 成功了。当我尝试自动生成单元测试时,VS'08 仍然想创建 Web 服务测试,但我可以手动创建我想要的测试,并且它们的行为符合预期。
    • 太棒了。就个人而言,我希望他们能一起从 VS 中删除网站类型的项目。 ;) 很高兴我能帮上忙。
    【解决方案2】:

    要在网站项目中测试 WCF 服务,您还可以使用“添加服务引用”将服务添加到您的测试项目中。作为 URL,输入http://localhost:4324/...?wsdl url。

    现在你可以写一个测试方法了:

    [TestClass()]
    public class MyServiceTest
    {
        #region -- Webservice initialisation
    
        // The client object for the WCF service.
        private static MyServiceNamespace.MyServiceClient _service;
    
        [ClassInitialize]
        public static void InitializeClass(TestContext ctx)
        {
            _service = new MyServiceNamespace.MyServiceClient();
        }
    
        [ClassCleanup]
        public static void CleanupClass()
        {
            _service.Close();
        }
    
        #endregion
    
    
        // An example test
        [TestMethod()]
        public void GetBlaBlaTest()
        {
            var actual = _service.GetBlaBla();
            Assert.IsTrue(actual.Status.IsSuccess, "Call should return success status");
            Assert.AreEqual("blabla", actual.Result, "Call should return the string 'blabla'");
        }
    }
    

    项目启动后,单元测试应用程序和网站项目都将启动,测试将针对 ASP.NET 开发服务器运行。这样做的好处是任何初始化代码也可以正常运行,模拟服务的实际使用。

    【讨论】:

    • 问题不在于 WCF Web 服务。
    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 2011-04-30
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多