您当前所在位置:首页攻略.NET 8 Moq mock GetRequiredKeyedService Setup报错

.NET 8 Moq mock GetRequiredKeyedService Setup报错

更新:2024-08-31 10:12:29编辑:游戏资讯归类:攻略

项目代码里有地方用到 IServiceProvider.GetRequiredKeyedService<T> 来解析服务。在编写单元测试时需要对其进行Mock处理,本以为像下面这样写就可以:

var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(x => x.GetRequiredKeyedService<AAA>(It.IsAny<BBB>())).Returns(new CCC());

然而,却出现了报错:

  Test method threw exception: 
  System.NotSupportedException: Unsupported expression: x => x.GetRequiredKeyedService(It.IsAny<Type>(), It.IsAny<object>())
  Extension methods (here: ServiceProviderKeyedServiceExtensions.GetRequiredKeyedService) may not be used in setup / verification expressions.
  ...

这让人有点奇怪,难道 GetRequiredKeyedService 不是接口方法吗?查看.NET源代码后发现, GetRequiredKeyedService 实际上是 IServiceProvider 的扩展方法,而我们知道Moq是不支持Setup扩展方法的。

/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="provider">The <see cref="IServiceProvider"/> to retrieve the service object from.</param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns>A service object of type <typeparamref name="T"/>.</returns>
/// <exception cref="System.InvalidOperationException">There is no service of type <typeparamref name="T"/>.</exception>
public static T GetRequiredKeyedService<T>(this IServiceProvider provider, object? serviceKey) where T : notnull
{
    ThrowHelper.ThrowIfNull(provider);

    return (T)provider.GetRequiredKeyedService(typeof(T), serviceKey);
}

原因找到后,解决起来就容易多了。翻看源码,一步步找到 IServiceProvider.GetRequiredKeyedService 最终调用的接口方法,然后再进行Mock即可。

首先看下 requiredServiceSupportingProvider.GetRequiredKeyedService(serviceType, serviceKey) 调用的是哪个方法:

  /// <summary>
  /// IKeyedServiceProvider is a service provider that can be used to retrieve services using a key in addition
  /// to a type.
  /// </summary>
  public interface IKeyedServiceProvider : IServiceProvider
  {
      /// <summary>
      /// Gets the service object of the specified type.
      /// </summary>
      /// <param name="serviceType">An object that specifies the type of service object to get.</param>
      /// <param name="serviceKey">An object that specifies the key of service object to get.</param>
      /// <returns> A service object of type serviceType. -or- null if there is no service object of type serviceType.</returns>
      object? GetKeyedService(Type serviceType, object? serviceKey);

      /// <summary>
      /// Gets service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/> implementing
      /// this interface.
      /// </summary>
      /// <param name="serviceType">An object that specifies the type of service object to get.</param>
      /// <param name="serviceKey">The <see cref="ServiceDescriptor.ServiceKey"/> of the service.</param>
      /// <returns>A service object of type <paramref name="serviceType"/>.
      /// Throws an exception if the <see cref="IServiceProvider"/> cannot create the object.</returns>
      object GetRequiredKeyedService(Type serviceType, object? serviceKey);
  }

可以看到 IKeyedServiceProvider 也是继承了 IServiceProvider 接口,这就更好办了。我们直接Mock IKeyedServiceProvider 再进行Setup即可,将用到 IServiceProvider 的地方,换成 IKeyedServiceProvider

代码如下:

var serviceProvider = new Mock<IKeyedServiceProvider>();
serviceProvider.Setup(x => x.GetRequiredKeyedService(It.IsAny<AAA>(), It.IsAny<BBB>())).Returns(new CCC());

运行测试,一切完美。

总结

解决这个问题并不困难,但是如果.Net不开源,看不到源代码,还是有点头疼。

以上就是电脑114游戏给大家带来的关于.NET 8 Moq mock GetRequiredKeyedService Setup报错全部内容,更多攻略请关注电脑114游戏。

电脑114游戏-好玩游戏攻略集合版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

星球大战亡命之徒一修大师修改器下载地址 剧情重构密室解密