首页 > 解决方案 > 我是否需要测试前端客户端应用程序进行的实际 API 调用?

问题描述

我对 C# 和 JavaScript 比较陌生,我正在制作一个 ASP.NET/React/Redux 应用程序。我使用 superagent 编写了一个简单的 HTTP 发布请求:

import request from "superagent";

export const authenticationService = {
    login
};

function login(email, password) {
    return request.post("http://localhost:55903/api/login/contractor")
                  .type("form")
                  .send({ EmailAddress: email})
                  .send({ Password: password })
                  .then((res) => {
                      return res;
                  })
                  .catch((err) => {
                      return err;
                  });
};

我的主要问题是我是否需要对实际登录功能本身进行单元测试?到目前为止,我只看到了一些模拟 HTTP 响应来测试 React/Redux 方面的示例,但我还没有看到测试上述 HTTP 请求函数的示例。

标签: javascriptreactjsunit-testingasp.net-web-api

解决方案


在您的情况下,在前端最好模拟数据,因为您不是在测试 HTTP 请求,而是在测试您的 React 应用程序是否显示正确的内容。

然而,应该在后端测试 HTTP 请求。所以你的登录请求测试应该用你的 C# 代码而不是你的 React/Redux 代码。


推荐阅读