Moq.netcore 在 .Net Core RC2 上失败

16

我之前在 .Net RC1 上使用 Moq 完成了一个解决方案,现在已经升级到了 RC2 版本,发现 Moq.netcore 可以在新平台上运行。

我在 NuGet.config 中添加了 aspnet-contrib。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="contrib" value="https://www.myget.org/F/aspnet-contrib/api/v3/index.json" />
    <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

我已经在我的project.json文件中添加了moq.netcore。

"dependencies": {
  "Microsoft.NETCore.App": {
    "version": "1.0.0-rc2-*",
    "type": "platform"
  },
  "dotnet-test-xunit": "1.0.0-rc2-173361-36",
  "moq.netcore": "4.4.0-beta8",
  "xunit": "2.1.0"
},

"testRunner": "xunit",

"frameworks": {
  "netcoreapp1.0": {
    "imports": [
      "dotnet5.6",
      "portable-net451+win8"
    ]
  }
}

基本上,我按照Cli Testing Abstractions UnitTests的步骤进行操作,但在实例化一个Mock对象时遇到了以下错误:
System.IO.FileNotFoundException : 
  Could not load file or assembly 'System.Diagnostics.TraceSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

  Stack Trace:
       at Castle.DynamicProxy.Generators.MethodWithInvocationGenerator.BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter class, ProxyGenerationOptions options, INamingScope namingScope)
       at Castle.DynamicProxy.Generators.MethodGenerator.Generate(ClassEmitter class, ProxyGenerationOptions options, INamingScope namingScope)
       at Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementMethod(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
       at Castle.DynamicProxy.Contributors.CompositeTypeContributor.Generate(ClassEmitter class, ProxyGenerationOptions options)
       at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateType(String name, Type[] interfaces, INamingScope namingScope)
       at Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory)
       at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
       at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
       at Moq.Mock`1.<InitializeInstance>b__19_0()
       at Moq.Mock`1.OnGetObject()
       at Moq.MockDefaultValueProvider.ProvideDefault(MethodInfo member)
       at Moq.QueryableMockExtensions.FluentMock[T,TResult](Mock`1 mock, Expression`1 setup)
       at lambda_method(Closure )
       at Moq.Mock.GetInterceptor(Expression fluentExpression, Mock mock)
       at Moq.Mock.<>c__DisplayClass57_0`2.<SetupGet>b__0()

请在project.json文件中添加更多的组件,以"Microsoft.Extensions.Logging.TraceSource"开头。 - Jeroen Heier
1个回答

30

编辑:通过Moq > 4.6.38-alpha,这个技巧已经不再必要了。

"dependencies" {
  "Moq": "4.6.38-alpha"
}
这个bug可能是由于moq包并没有直接引用System.Diagnostics.TraceSource导致的,因此它在你的项目中不会被传递地导入。为了解决这个问题,你可以显式地引用System.Diagnostics.TraceSource包:以下是我们在OAuth2验证中间件测试项目中使用的方法,该项目在.NET Desktop和.NET Core上运行:
{
  "buildOptions": {
    "warningsAsErrors": true
  },

  "dependencies": {
    "AspNet.Security.OAuth.Validation": { "target": "project" },
    "dotnet-test-xunit": "1.0.0-rc2-build10015",
    "Microsoft.AspNetCore.TestHost": "1.0.0-rc2-final",
    "Microsoft.Extensions.Caching.Memory": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Newtonsoft.Json": "8.0.3",
    "xunit": "2.1.0"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0-rc2-3002702" },
        "moq.netcore": "4.4.0-beta8",
        "System.Diagnostics.TraceSource": "4.0.0-rc2-24027"
      },

      "imports": [
        "dnxcore50",
        "portable-net451+win8"
      ]
    },

    "net451": {
      "dependencies": {
        "Microsoft.NETCore.Platforms": "1.0.1-rc2-24027",
        "Moq": "4.2.1312.1622"
      }
    }
  },

  "testRunner": "xunit"
}

https://github.com/aspnet-contrib/AspNet.Security.OAuth.Extensions/blob/master/test/AspNet.Security.OAuth.Validation.Tests/project.json#L21


嗯...我尝试了System.Diagnostics,但是这很有道理。看起来有点奇怪,但我会把它归因于Moq处于beta版的原因。感谢您的帮助。 - Saqib Rokadia
1
System.Diagnostics.TraceSource可以在Nuget包管理器中找到,如果选中了“包括预发行版”,则会包含该程序包。 - Tyson Nero
1
编辑说明:在 Moq 4.6.38-alpha 中不再需要使用这个技巧。 - Kévin Chalet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接