Expression references a method that does not belong to the mocked object

后端 未结 4 1508
耶瑟儿~
耶瑟儿~ 2020-12-05 04:44

I have an api service that calls another api service. When I set up the Mock objects, it failed with an error:

NotSupportedException: expression refer

4条回答
  •  死守一世寂寞
    2020-12-05 04:54

    Since the question about mocking IConfiguration is referring this answer as duplicate, I will contribute my dime here. This is how I "Mock" IConfiguration, which is, in my opinion, a bit cleaner:

        private static IConfiguration GetConfigurationMock(string jsonConfiguration)
        {
            var byteArray = Encoding.UTF8.GetBytes(jsonConfiguration);
            var stream = new MemoryStream(byteArray);
    
            var conf = new ConfigurationBuilder();
            conf.AddJsonStream(stream);
            var confRoor = conf.Build();
    
            return confRoor; 
        } 
    

    Original question: How to setup Mock of IConfigurationRoot to return value

提交回复
热议问题