【问题标题】:Service error when running my Unit test运行我的单元测试时出现服务错误
【发布时间】:2016-02-13 02:32:30
【问题描述】:

我最初写了一个单元测试失败,现在我已经解决了这个问题,但是当我尝试再次运行我的单元测试时,我得到了这个错误

System.InvalidOperationException:在 ServiceModel 客户端配置部分中找不到引用合同“InventoryManager.Services.IUserService”的默认端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此协定匹配的端点元素。

我不知道为什么会发生这种情况,因为我在单元测试 App.Config 中使用与我的服务库 App.Config 相同的配置,并且合同名称相同。

服务库项目 App.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="InventoryManager.Services.UserService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:3637/Service.svc" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="basicHttpBinding" contract="InventoryManager.Services.IUserService">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <connectionStrings>
    <add name="InventoryManagerEntities" connectionString="metadata=res://*/InventoryManagerModel.csdl|res://*/InventoryManagerModel.ssdl|res://*/InventoryManagerModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>

单元测试项目 App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="InventoryManager.Services.UserService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:3637/Service.svc?wsdl" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="basicHttpBinding" contract="InventoryManager.Services.IUserService">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <connectionStrings>
    <add name="InventoryManagerEntities" connectionString="metadata=res://*/InventoryManagerModel.csdl|res://*/InventoryManagerModel.ssdl|res://*/InventoryManagerModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>

【问题讨论】:

  • baseAddress 略有不同,是故意的吗?
  • 单元测试应该独立于任何外部资源,如数据库、服务等。
  • 请注意,您有一个 WCF 服务 。您需要将库的 app.config 中的 &lt;system.serviceModel&gt; 部分复制到托管服务的应用程序的 web/app.config。甚至库项目模板也包含一个 app.config 文件,库使用它 - 它们使用正在使用该库的应用程序的 web/app.config。
  • @MartinMulder 你能解释一下是什么让我的单元测试依赖于服务或数据库吗?...据我了解,我正在测试 ma 控制器。它是调用我的服务的控制器
  • 让我更清楚一点:单元测试不应该直接或间接调用数据库或服务。

标签: c# wcf unit-testing asp.net-mvc-4 invalidoperationexception


【解决方案1】:

单元测试的应用配置文件应该有一个&lt;clients&gt; 部分,而不是&lt;services&gt; 部分。

从单元测试的配置文件中删除 &lt;services&gt; 部分,并将其替换为以下内容:

<client>
  <endpoint address="http://localhost:3637/Service.svc" 
            binding="basicHttpBinding" 
            contract="InventoryManager.Services.IUserService">
</client>

&lt;services&gt; 部分用于定义服务应用程序的服务,而&lt;client&gt; 部分用于定义客户端将连接到的服务。

【讨论】:

  • 嗨蒂姆,非常感谢你,这很有效......我只有一个问题,关于上面的一个 cmets。 “单元测试应该独立于数据库和服务”......在我的测试项目中使用这个 app.config 是否使它依赖于服务?
  • @ifelabolz - 我不确定,因为我对单元测试没有那么丰富的经验(我们今年才开始在工作中进行)。您的单元测试本质上指向服务使其具有依赖性,但我不知道您将如何打破这种依赖性。
  • 这里有个愚蠢的问题。发现这个讨论试图用我的单元测试项目解决同样的错误。从测试项目的 App.config 复制 system.serviceModel 有所帮助,但我想知道这是否可以以某种方式自动化。有没有办法告诉VS将此部分自动导入单元测试项目?或者也许有一种方法可以在测试运行之前动态导入它?
猜你喜欢
  • 2017-07-21
  • 2013-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 2015-11-21
  • 2014-11-09
相关资源
最近更新 更多