Moq.netcore无法使用.Net Core RC2

时间:2016-05-18 00:39:52

标签: c# .net asp.net-core moq .net-core-rc2

所以我在Moq上使用.Net RC1解决了这个问题,并且我已经升级到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>

我已将moq.netcore添加到我的project.json文件中。

"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()

1 个答案:

答案 0 :(得分:29)

修改: Moq > 4.6.38-alpha不再需要这个技巧:

The list inserted as [biggest,secondBiggest] after calling main is: [real, to]

此错误可能是由于事件"dependencies" { "Moq": "4.6.38-alpha" } 未被moq包直接引用而导致的,因此不会在您的项目中以传递方式导入。要解决此限制,您可以明确引用System.Diagnostics.TraceSource包:

以下是我们如何在我们的OAuth2验证中间件测试项目中使用它,该项目在.NET桌面和.NET Core上运行:

System.Diagnostics.TraceSource

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