RhinoMocks模拟RestSharp客户端

时间:2019-12-11 10:58:38

标签: c# mocking restsharp rhino-mocks

我在使用RhinoMocks和Nunit从RestSharp创建RestClient实现的简单模拟时遇到麻烦。

我正在将RestClient和一些配置值注入到sut中。

  public class MyRestApiAbstraction : IRestApi
  {
        private const string BaseUrl = "basepath";

        private readonly IRestClient _restClient;

        public MyRestApiAbstraction (IOptions<MyConfig> configuration, IRestClient restClient)
        {
            var conf = configuration;
            _restClient = restClient;
            _restClient.Authenticator =
                new HttpBasicAuthenticator(conf.Value.Username,
                    conf.Value.Password);
            _restClient.BaseUrl = new Uri(conf.Value.Hostname + BaseUrl, UriKind.Absolute);
        }

        public IRestResponse<T> Execute<T>(RestRequest request) where T : new()
        {
            return IsSuccessful(_restClient.Execute<T>(request));
        }

        private static IRestResponse<T> IsSuccessful<T>(IRestResponse<T> response)
        {
            if (!response.StatusCode.Equals(HttpStatusCode.OK))
                throw new CustomException1(response.StatusCode);

            if (response.ResponseStatus == ResponseStatus.TimedOut || response.ResponseStatus == ResponseStatus.Aborted)
                throw new CustomException3("test");

            return response;
        }
    }

在startup.cs中,我像这样解决ÌRestClient依赖项:

services.AddScoped<IRestClient>(provider => new RestClient());

然后在测试中,我将依赖关系创建为模拟对象并创建sut对象。

   [Test]
   public void Execute_Successful()
   {
       //Arrange
       var request = new RestRequest();
       var response = new RestResponse<Wrapper<Model>>
           {StatusCode = HttpStatusCode.OK, ResponseStatus = ResponseStatus.Completed};
       var restClient = MockRepository.GenerateMock<RestClient>();
       restClient.Stub(client =>
          client.Execute<Wrapper<Model>>(Arg<RestRequest>.Is.Anything))
          .Return(response);
       var myRestApiAbstraction = new MyRestApiAbstraction (Options.Create(_config), restClient);

       //Act 
       sapRestApi.Execute<SapResponseWrapper<OfferOfCompromiseProtocol>>(request);

       //Assert
   }

当我尝试运行测试时,出现此异常:

SapVangProcessing.UnitTests.SapRestApiTests.Execute_Successful

System.MissingMethodException : Can't find a constructor with matching arguments
----> System.MissingMethodException : Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
at Rhino.Mocks.MockRepository.MockClass(CreateMockState mockStateFactory, Type type, Type[] extras, Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.DynamicMock[T](Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.CreateMockInReplay[T](Func`2 createMock)
at SapVangProcessing.UnitTests.SapRestApiTests.Execute_Successful() in Mytestproject.UnitTests\Test.cs:line 33
--MissingMethodException
at Castle.DynamicProxy.ModuleScope.CreateModule(Boolean signStrongName)
at Castle.DynamicProxy.ModuleScope.ObtainDynamicModuleWithStrongName()
at Castle.DynamicProxy.ModuleScope.ObtainDynamicModule(Boolean isStrongNamed)
at Castle.DynamicProxy.Generators.Emitters.ClassEmitter.CreateTypeBuilder(ModuleScope modulescope, String name, Type baseType, IEnumerable`1 interfaces, TypeAttributes flags, Boolean forceUnsigned)
at Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope, String name, Type baseType, IEnumerable`1 interfaces, TypeAttributes flags, Boolean forceUnsigned)
at Castle.DynamicProxy.Generators.BaseProxyGenerator.BuildClassEmitter(String typeName, Type parentType, IEnumerable`1 interfaces)
at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateType(String newName, Type[] interfaces)
at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
at Rhino.Mocks.MockRepository.MockClass(CreateMockState mockStateFactory, Type type, Type[] extras, Object[] argumentsForConstructor)

如果我查看RestClient的来源,就会发现它具有默认的构造函数。

我在这里做什么错了?

0 个答案:

没有答案