注册Moq实例时,Autofac会抛出File Not Found异常

时间:2014-12-20 02:50:29

标签: c# unit-testing windows-store-apps moq autofac

我创建了一个Windows 8.1 App Store单元测试项目,添加了Moq和Autofac并尝试使用Autofac注册Mock<IAccountService>。当我调用RegisterInstance(serviceMock.Object)时,Autofac会抛出File Not Found异常

界面如下所示:

public interface IAccountService
{
    Task<OperationResult> CreateAccount(User user, string password, string passwordConfirmation);

    Task<OperationResult<User>> Signin(string username, string password);
}

我用

设置了我的模拟
// Set up our mocked repositories and services
var accountServiceMock = new Mock<IAccountService>();
accountServiceMock
    .Setup(service => service.CreateAccount(
        It.IsAny<User>(),
        It.IsAny<string>(),
        It.IsAny<string>()))
    .Returns((User user, string password, string passwordConfirmation) =>
        {
            var taskCompletion = new TaskCompletionSource<OperationResult>();
            taskCompletion.SetResult(new OperationResult(true));
            return taskCompletion.Task;
        });

accountServiceMock
    .Setup(service => service.Signin(It.IsAny<string>(), It.IsAny<string>()))
    .Returns((string username, string password) =>
        {
            var taskCompletion = new TaskCompletionSource<OperationResult<User>>();
            taskCompletion.SetResult(
                new OperationResult<User>(true, new User { Username = username }));

            return taskCompletion.Task;
        });

当我使用Autofac注册模拟时,抛出异常

var builder = new ContainerBuilder();

// Exception thrown here.
builder.RegisterInstance(accountServiceMock.Object).As<IAccountService>();
IContainer container = builder.Build();

如果我使用此代码并围绕它构建标准的非App Store单元测试项目,则单元测试运行时没有问题,构建器会注册模拟实例。

我还要指出我可以使用RegisterType()。As();没有任何问题。我也可以使用RegisterInstance一个具体的,非模拟的对象,没有任何例外。

在Windows 8.1项目中,我是否遗漏了这些内容?

异常

[System.IO.FileNotFoundException]   
{"Could not load file or assembly 'ISymWrapper, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. 
The system cannot find the file specified.":"ISymWrapper, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}

堆栈跟踪

 at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)\r\n
 at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)\r\n   
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)\r\n   
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)\r\n   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)\r\n   
at System.Reflection.Assembly.Load(String assemblyString)\r\n   
at System.Reflection.Emit.AssemblyBuilder.LoadISymWrapper()\r\n   
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModuleInternalNoLock(String name, Boolean emitSymbolInfo, StackCrawlMark& stackMark)\r\n   
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModuleInternal(String name, Boolean emitSymbolInfo, StackCrawlMark& stackMark)\r\n   
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule(String name, Boolean emitSymbolInfo)\r\n   
at Castle.DynamicProxy.ModuleScope.CreateModule(Boolean signStrongName)\r\n   
at Castle.DynamicProxy.ModuleScope.ObtainDynamicModuleWithWeakName()\r\n   
at Castle.DynamicProxy.ModuleScope.ObtainDynamicModule(Boolean isStrongNamed)\r\n   
at Castle.DynamicProxy.Generators.Emitters.ClassEmitter.CreateTypeBuilder(ModuleScope modulescope, String name, Type baseType, Type[] interfaces, TypeAttributes flags, Boolean forceUnsigned)\r\n   
at Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope, String name, Type baseType, Type[] interfaces, TypeAttributes flags, Boolean forceUnsigned)\r\n   
at Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope, String name, Type baseType, Type[] interfaces, TypeAttributes flags)\r\n   
at Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope, String name, Type baseType, Type[] interfaces)\r\n   
at Castle.DynamicProxy.Generators.BaseProxyGenerator.BuildClassEmitter(String typeName, Type parentType, Type[] interfaces)\r\n   
at Castle.DynamicProxy.Generators.BaseProxyGenerator.BuildClassEmitter(String typeName, Type parentType, IList interfaceList)\r\n   
at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)\r\n   
at Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)\r\n   
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)\r\n   
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)\r\n   
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, IInterceptor[] interceptors)\r\n   
at Moq.Mock`1.<InitializeInstance>b__0()\r\n   
at Moq.PexProtector.Invoke(Action action)\r\n   
at Moq.Mock`1.InitializeInstance()\r\n   
at Moq.Mock`1.get_Object()\r\n   
at Mobile.Windows.Shared.Tests.Bootstrapper.Setup() 

1 个答案:

答案 0 :(得分:2)

我刚刚发布了另一个可能对您有帮助的问题的答案。它将为您提供一个可以轻松与Windows Store应用程序一起使用的备用模拟框架的链接。

我目前正在使用Unity作为DI容器和Prism。

Telerik JustMock answer