简体   繁体   English

对运行时会击中应用程序的API调用进行单元测试的最佳方法

[英]Best way to unit test an API call that hits the application when running

Right now we are working on building a custom CMS but I am running into a roadblock where I can't quite figure out the best approach with a piece of it to allow for good unit testing. 现在,我们正在构建自定义CMS,但是我遇到了一个障碍,在其中我无法完全理解采用最佳方法进行最佳单元测试的最佳方法。

Our process is as follows: 我们的过程如下:

The request comes in --> Parse the page for components --> Found component --> Hit the component API to get the component content 请求进入->解析组件页面->找到的组件->点击组件API以获取组件内容

The only issue is when unit testing we hit the local URL for the app, which won't work since the app isn't fully running when running tests. 唯一的问题是,在单元测试中,我们点击了应用程序的本地URL,由于运行测试时应用程序未完全运行,因此无法使用。

Component Rendering Code: 组件渲染代码:

var componentBody = await httpClient
            .GetStringAsync("http://localhost:5001/Api/Components/" + ComponentName + "/View/" + ComponentId);
ComponentStructure componentStructure = this.ParseHtml(componentBody);

Unit Testing Code: 单元测试代码:

public async Task TestPageParsing()
    {
        this.pagesClient = this.clientManager.GetClient<Page, PageData>("pages");
        var pages = await this.pagesClient.GetAsync();
        this.pageParsingService.FindComponents(pages.Items[0].Data.Text);
    }

If you're trying to test the behavior and not the underlying platform, a unit test is all you need. 如果您要测试行为而不是基础平台,则只需要单元测试。 You do this by mocking out your platform dependencies (database, http, et cetera) and replacing them with mocks that return hard coded facts. 为此,您可以模拟平台依赖项(数据库,http等),并用返回硬编码事实的模拟替代它们。 Leave the platform specifics to manual testing. 将平台细节留给手动测试。 If you really want to write an integration test, it should still be automated in your test framework; 如果您确实想编写一个集成测试,则仍应在您的测试框架中将其自动化。 not requiring manual setup for a developer to run the test. 不需要手动设置即可让开发人员运行测试。 Categorize these separately though so people can run your fast unit tests separately from the slow and long running integration tests. 但是,将它们分别分类,以便人们可以将运行缓慢的单元测试与运行缓慢的长时间集成测试分开运行。

It sounds like behavior is what you want. 听起来行为就是您想要的。 For instance, your http web api returns back a blob of html that you want to parse. 例如,您的http Web api返回要解析的html块。 You don't even need a mock in this case, if you're just trying to test that ParseHtml works. 如果您只是想测试ParseHtml有效,在这种情况下甚至不需要模拟。

var html = "<html />";

var result = myObj.ParseHtml(html);

// Make some assertion here

You can write multiple test cases based on expected html, and you can also add test cases for potential malformatted html, or other unexpected scenarios to ensure ParseHtml behaves as expected. 您可以根据预期的html编写多个测试用例,还可以为潜在的格式错误的html或其他意外情况添加测试用例,以确保ParseHtml行为符合预期。

Looks like this is an integration test instead of a unit test. 看起来这是一个集成测试而不是单元测试。

You could mock out the call to the application and present the calling function with a fixed response instead, and verify that it renders correctly. 您可以模拟对应用程序的调用,并使用固定的响应呈现调用函数,并验证其是否正确呈现。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM