简体   繁体   English

C#UnitTest异步方法错误 - System.MissingMethodException:找不到方法:

[英]C# UnitTest Async Method Error - System.MissingMethodException: Method not found:

Hi I have a window services project that I already successfully implement to the server. 嗨,我有一个窗口服务项目,我已经成功地实现了服务器。 But I have to create unit test which I stuck for weeks to solve it. 但是我必须创建单元测试,我坚持了数周才能解决它。 Can any of you guys help me? 你们有谁能帮我吗?

  • I have clean the project several time and the issue come out when I try to setup the Mock based on my interface 我已经多次清理项目了,当我尝试根据我的界面设置Mock时问题就出来了

  • I also have refactoring the code several time but fail to run the unittest :( 我也有几次重构代码,但无法运行unittest :(

Here is my code: 这是我的代码:

Interface 接口

public interface IJobScheduler
{
    Task<HttpResponseMessage> GetASync(HttpClient client, string destination, CancellationTokenSource cancelToken);
    Task<bool> RunJobAgent(HttpClient client);
}

Class (purposely create to inject data using unit test) (故意创建使用单元测试注入数据)

public class JobSchedular
{
    private IJobScheduler iJobScheduler;

    public JobSchedular(IJobScheduler ijobscheduler) => iJobScheduler = ijobscheduler;

    public JobSchedular() => iJobScheduler = new JobSchedularSvc();
    public async Task<HttpResponseMessage> GetASync(HttpClient client, string destination, CancellationTokenSource cancelToken)
  {
    Task<HttpResponseMessage> result = iJobScheduler.GetASync(client, destination, cancelToken);
    return await result;
  }
}

Actual Class 实际班级

    public partial class JobSchedularSvc : ServiceBase, IJobScheduler
    {   
        public async Task<HttpResponseMessage> GetASync(HttpClient client, string destination, CancellationTokenSource cancelToken)
        {
            try
            {
                HttpResponseMessage response;// = new HttpResponseMessage();
                using (client)
                {
                    response = await client.GetAsync(destination, cancelToken.Token);
                }

                return response;
            }
            catch (Exception ex)
            {
                LogHandler.InsertLog(LogLevel.Error, $"FAILED: GetAsync() - {ex.Message}", ex.StackTrace, "JobSchedulerSvc", "JobSchedulerSvc", null, null, null, null).Wait();
                return null;
            }

        }
}

Test Method 测试方法

   public async Task Test()
    {
        var message = new HttpResponseMessage(HttpStatusCode.OK);
        JobScheduler = new Mock<IJobScheduler>();

        JobScheduler.Setup(test => test.GetASync(It.IsAny<HttpClient>(), It.IsAny<string>(), It.IsAny<CancellationTokenSource>()))
            .Returns(Task.FromResult(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("{'Result':true,'Exception':[]}") }));

        CancellationTokenSource cancelToken = new CancellationTokenSource();

        var response = new JobSchedular(JobScheduler.Object).GetASync(new HttpClient(), "/api/job/runjobagent", cancelToken);

        var result = await response.Result.Content.ReadAsStringAsync();

        Assert.AreEqual(result, "{'Result':true,'Exception':[]}");
    }

I just want to call the local function GetAsync() which returns httpResponseMessage 我只想调用返回httpResponseMessage的本地函数GetAsync()

I edit the .csproj file and add this line it works 我编辑.csproj文件并添加它的工作线

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.MissingMethodException:找不到方法? - System.MissingMethodException: Method not found? System.MissingMethodException:找不到 Bigquery 的方法 - System.MissingMethodException: Method not found for Bigquery 运行到System.MissingMethodException:使用PrivateObject找不到方法 - Running into System.MissingMethodException: Method Not Found with PrivateObject 找不到System.MissingMethodException方法Crystal报表 - System.MissingMethodException Method Not Found Crystal Reports dll 中的方法抛出 System.MissingMethodException:找不到方法 - Method in dll throwing System.MissingMethodException: Method not found System.MissingMethodException: &#39;找不到 XmlRpcProxyGen.Create 的方法 - System.MissingMethodException: 'Method not found for XmlRpcProxyGen.Create sqlite3中的c#System.MissingMethodException - c# System.MissingMethodException in sqlite3 System.MissingMethodException错误 - System.MissingMethodException error System.MissingMethodException:找不到方法:“?” 尝试使用委托方法构建自定义动态类型时 - System.MissingMethodException: Method not found: '?' when trying to build a custom dynamic type with a delegate method System.MissingMethodException:找不到方法:&#39;System.String .get_DayName()&#39; - System.MissingMethodException: Method not found: 'System.String .get_DayName()'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM