我有一个带有 Jest 测试套件的 React 应用程序。应用程序使用 redux,测试套件使用 redux-mock-store。我正在使用 react-thunk 中间件来延迟调度操作,因为应用程序需要与远程 Firebase 数据库同步数据。我希望我的测试套件在向 Redux 分派操作后验证某些条件,如下所示:
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
// This is my custom async action generator.
import { asyncAction } from './some/path';
const createMockStore = configureMockStore([thunk]);
test("Test", (done) => {
const store = createMockStore({});
const data = { ... };
store.dispatch(asyncAction(data)).then(() => {
expect(someCondition);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
该测试使用 Jest 返回的 done 处理程序来等待 store.dispatch 返回的 promise 完成。但是,promise 永远不会执行,测试进入无限循环,Jest 失败并出现以下异常:
Assertion failed: new_time >= loop->time, file c:\ws\deps\uv\src\win\core.c, line 309 …Run Code Online (Sandbox Code Playgroud) 我正在使用支持JSON格式的POST请求的RESTful API。API自己的Swagger文档显示这是对其端点之一的有效调用:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '<JSON>' '<URL>'
Run Code Online (Sandbox Code Playgroud)
其中<JSON>和<URL>是有效的JSON消息和端点的URL。从Swagger文档中,我收集到该端点的所有帖子都必须同时包含Content-Type和Accept设置为的标头application/json。
我正在编写一个C#方法,该方法将使用.NET Core的HttpClient类发布到此终结点。但是,发布消息后,我收到HTTP 415错误代码,表示不支持的媒体类型。根据到目前为止的了解,Content-Type必须在您的内容中设置标题(我正在使用StringContent该类),并且Accept只能在HttpClient标题中设置标题。这是我的特定示例:
var httpContent = new StringContent("<JSON>", Encoding.UTF32, "application/json");
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var responseMessage = httpClient.PostAsync("<URL>", httpContent);
var result = responseMessage.Result;
Run Code Online (Sandbox Code Playgroud)
再一次,在<JSON>和<URL>是有效JSON消息以及端点的URL。在我看来,我引用的第三行httpCllient.DefaultRequestHeaders没有在Accept: application/json请求中添加标题。如果我手动将标头添加到httpContent.Headers集合中,Accept则会收到运行时错误消息,告诉我不是我可以添加到标头中的标头httpContent。这就是为什么我希望将其添加到httpClient。
我已经使用Swagger验证了URL和JSON,所以我知道它们是正确的。另外,请求是通过HTTPS完成的,所以我无法使用Fiddler来验证 …
.net-core ×1
c# ×1
firebase ×1
http-error ×1
http-headers ×1
jestjs ×1
react-redux ×1
reactjs ×1
redux-thunk ×1