无法解析类型名称。请检查配置文件

时间:2013-02-18 11:51:41

标签: unit-testing unity-container moq unity2.0

尝试使用moq和unity对我的应用程序进行单元测试,并且我在哪里遇到错误 service是一个接口,无法解析:

错误讯息:

    Test method Ecommerce_Test.Tests.LoginSuccessfulRedirectToActionLoad threw exception: 
System.InvalidOperationException: The type name or alias Ecommerce_Test.AuthenticationMock could not be resolved. Please check your configuration file and verify this type name.

错误堆栈跟踪:

    Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs: line 110
Microsoft.Practices.Unity.Configuration.RegisterElement.GetMappedType() in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs: line 128
Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs: line 0
Microsoft.Practices.Unity.Configuration.ContainerElement.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs: line 114
Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach[TItem](IEnumerable`1 sequence, Action`1 action) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Utility\EnumerableExtensions.cs: line 36
Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs: line 110
Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityConfigurationSection.cs: line 151
Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityContainerExtensions.cs: line 35
Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityContainerExtensions.cs: line 63
Ecommerce.Controllers.HomeController.Login(LoginModel model) in C:\Users\avmin!\Documents\Visual Studio 2010\Projects\Ecommerce_Mock\Ecommerce\Controllers\HomeController.cs: line 45
Ecommerce_Test.Tests.LoginSuccessfulRedirectToActionLoad() in C:\Users\avmin!\Documents\Visual Studio 2010\Projects\Ecommerce_Mock\Ecommerce_Test\Tests.cs: line 80

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

    <assembly  name="Ecommerce"/>
    <assembly  name="Ecommerce_Tests"/>

    <container>
      <register type="Ecommerce.Authentication.IAuthentication"
                mapTo="Ecommerce_Test.AuthenticationMock, Ecommerce_Test" />
    </container>
  </unity>
  <system.serviceModel>
      <bindings />
      <client />
  </system.serviceModel>
</configuration>

用法:

public ActionResult Login(HomeModels.LoginModel model)
        {
            var unity = new UnityContainer().LoadConfiguration(); //error fired here!!!
            var proxy = unity.Resolve<IAuthentication>();


            if (ModelState.IsValid)
            {
                try
                {
                    Login log = new Login();
                    log.Username = model.Username;
                    log.Password = model.Password;
                    int result = proxy.Login(new Login { Username = log.Username, Password = log.Password });

                }
                catch (MemberAccessException e)
                {
                    ModelState.AddModelError("Login was not successful", e);
                }
            }
            return View("Login",model);
        }

2 个答案:

答案 0 :(得分:10)

解决方案结果是将程序集的名称附加到结尾处 mapTo值。Ecommerce_Tests project > Properties配置文件在上面更新以反映这一点。

右键点击{{1}}找到的程序集名称。

答案 1 :(得分:0)

另外,如果你为注入的代码使用单独的dll,请确保目标框架是相同的,并且包含注入类的dll被复制到run(debug)文件夹