我可以将FlurlClient与Asp.Net Core TestServer一起使用吗? [英] Can I use FlurlClient with Asp.Net Core TestServer?

查看:250
本文介绍了我可以将FlurlClient与Asp.Net Core TestServer一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在一些项目中使用FlurlClient,并熟悉其流畅的界面. 现在,我们希望在使用TestServer的asp.net核心集成测试中使用它. 来自的例子 http://asp.net-hacker.rocks/2017/09/27/testing-aspnetcore.html

We are using FlurlClient in a few projects and familiar with their fluent interface. We now want to use it in asp.net core integration tests using TestServer. The example from http://asp.net-hacker.rocks/2017/09/27/testing-aspnetcore.html

_server = new TestServer(new WebHostBuilder()
                             .UseStartup<Startup>());
_client = _server.CreateClient();

我打算将代码更改为

_server = new TestServer(new WebHostBuilder()
                             .UseStartup<Startup>());
var httpClient = _server.CreateClient();
_client = new FlurlClient(httpClient);

并使用所有FlurlClient方法/扩展.

and use all FlurlClient methods/extensions.

但是后来我注意到 是否可以使用Furl .Http和OWIN TestServer?,它描述了在实现OWIN时还需要做更多的工作.

But then I noticed Is it possible to use Furl.Http with the OWIN TestServer? which described that more work is required in owin implementation.

Asp.Net Core TestServer的方法是否相似?还是简化了?

Is approach for Asp.Net Core TestServer similar? Or is it simplified?

推荐答案

很多简化了,您建议的更改是完全正确的.您链接到的问题较旧,我的答案中包含的信息与2.x版本不再相关. (我已经对其进行了更新.)实际上,添加了直接在FlurlClient构造函数中提供现有HttpClient的功能,最近,并且牢记此特定用例.

It's much simplified, and your proposed change is exactly right. The question you linked to is old and my answer contains information that's no longer relevant in 2.x. (I have updated it.) In fact, the ability to provide an existing HttpClient directly in a FlurlClient constructor was added very recently, and with this specific use case in mind.

这是我用来替代CreateClient的扩展方法;如果您经常这样做,可能会觉得很方便:

Here's an extension method I use as a replacement for CreateClient; you might find it handy if you do this a lot:

public static class TestServerExtensions
{
    public static IFlurlClient CreateFlurlClient(this TestServer server) => 
        new FlurlClient(server.CreateClient());
}

这篇关于我可以将FlurlClient与Asp.Net Core TestServer一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆