首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C# UnitTest异步方法错误- System.MissingMethodException:找不到方法:

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

Stack Overflow用户
提问于 2019-03-18 02:37:28
回答 1查看 146关注 0票数 0

您好,我有一个window服务项目,我已经成功地在服务器上实现了它。但我必须创建单元测试,我坚持了几个星期才能解决它。你们有人能帮我吗?

  • 我已经清理了项目几次了,当我尝试基于我的接口
  • 设置模拟时出现了这个问题。我也重构了几次代码,但无法运行单元测试:(

下面是我的代码:

接口

代码语言:javascript
复制
public interface IJobScheduler
{
    Task<HttpResponseMessage> GetASync(HttpClient client, string destination, CancellationTokenSource cancelToken);
    Task<bool> RunJobAgent(HttpClient client);
}

(特意创建以使用单元测试注入数据)

代码语言:javascript
复制
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;
  }
}

代码语言:javascript
复制
    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;
            }

        }
}

代码语言:javascript
复制
   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':[]}");
    }

我只想调用返回httpResponseMessage的本地函数GetAsync()

EN

回答 1

Stack Overflow用户

发布于 2019-03-18 11:08:25

我编辑了.csproj文件并添加了下面这行,它可以工作

代码语言:javascript
复制
<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55210565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档