小编Bea*_*ter的帖子

在 .env 文件中设置 PUBLIC-URL React 应用程序

嗨,我是反应应用程序的新手。我想在 .env 文件中设置 Public_URL 。

.env 文件(我设置了 url 链接)

PUBLIC_URL="http://localhost:8000/api";
Run Code Online (Sandbox Code Playgroud)

Login.tsx 文件

const response = await fetch("/login", {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            credentials: 'include',
            body: JSON.stringify({
                userName,
                password
            })
            
        });
Run Code Online (Sandbox Code Playgroud)

当我启动应用程序时,我的应用程序应该可以正常工作。但在我的登录页面中,我将登录按钮我的 Url 调用提交到 http://localhost:3000/login。但我需要设置 http://localhost:8000/api 。我不知道如何设置它。请发送正确的示例。

包.json

 "scripts": {
    "start": "env-cmd -f .env react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
Run Code Online (Sandbox Code Playgroud)

reactjs react-redux

5
推荐指数
1
解决办法
1万
查看次数

如何在单元测试中模拟依赖注入对象

我的项目需要单元测试。我在控制器中使用构造函数依赖注入。当我在单元测试项目中模拟注入的依赖项对象并在测试方法中调用它时。在所有情况下都返回 null。

控制器类:

public class Owner:Controller
{
    private readonly IComRepository repository;
    private readonly DbContext context;
    
    public Owner(IComRepository repository, DbContext context)
    {
      this.repository=repository;
      this.context=context;
    }

    [HttpGet("GetAllTypes")]
    public async Task<IActionResult> GetAllTypes()
    {
      var ownerTypes=repository.GetTypes();
        return Ok(ownerTypes);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的存储库类

public Interface IComRepository
{
    IList<Type> GetTypes();
}
        
public Class ComRepository : IComRepository
{
    private readonly DbContext context;
    public ComRepository(DbContext context)
    {
        this.context=context;
    }
    public IList<Type> GetTypes()
    {
        var allTypes= context.Types.ToList();
        return allTypes;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我需要测试控制器类中的 GetAllTypes 方法。我的测试课如下:

using moq;
[TestClass]
public Class OwnerTest …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq mocking

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

mocking ×1

moq ×1

react-redux ×1

reactjs ×1

unit-testing ×1