Ember-CLI-Mirage 强制执行 JSON:API?
Ember-CLI-Mirage enforcing JSON:API?
遇到了几次失败,想知道我是否正确理解了 Mirage:
1.In ember-cli-mirage,我是否正确认为我定义的服务器响应应该反映我的实际服务器返回的内容?例如:
this.get('/athletes', function(db, request) {
let athletes = db.athletes || [];
return {
athletes: athletes,
meta: { count: athletes.length }
}
});
我正在使用自定义序列化程序,上面的内容与我的服务器响应此路由上的 get 请求的格式相匹配,但是,在两次测试中我遇到了两次失败并出现此错误:normalizeResponse must return a valid JSON API document: meta must be an object
2.Is mirage 强制执行 json:api 格式,这样做是因为我设置测试的方式吗?
例如,我有几个访问上述 /athletes
路由的测试,但是当我使用如下所示的异步调用时,我的失败发生了。我很想知道正确覆盖服务器响应行为的适当方法,以及为什么 normalizeResponse 错误出现在控制台中进行 2 次测试,但只会导致下面的测试失败。
test('contact params not sent with request after clicking .showglobal', function(assert) {
assert.expect(2);
let done = assert.async();
server.createList('athlete', 10);
//perform a search, which shows all 10 athletes
visit('/athletes');
fillIn('.search-inner input', "c");
andThen(() => {
server.get('/athletes', (db, request) => {
assert.notOk(params.hasOwnProperty("contacts"));
done();
});
//get global athletes, which I thought would now be intercepted by the server.get call defined within the andThen block
click('button.showglobal');
});
});
结果:
✘ Error: Assertion Failed: normalizeResponse must return a valid JSON API document:
* meta must be an object
expected true
我尝试按照上一个示例 here 中的建议将我的服务器响应更改为 json:api 格式,但这看起来与我的实际服务器响应完全不同,并导致我的测试失败,因为我的应用程序没有t 解析具有此结构的有效负载。必须感谢任何提示或建议。
你是对的。您上面显示的模拟是否发生故障?在我看来,它总是 return meta
作为一个对象,因此在发出请求后通过查看控制台来验证响应是否符合您的想法。
如果您想在测试期间看到回复,请在测试中输入 server.logging = true
:
test('I can view the photos', function() {
server.logging = true;
server.createList('photo', 10);
visit('/');
andThen(function() {
equal( find('img').length, 10 );
});
});
不,Mirage 不了解您的特定后端,尽管它确实带有一些默认设置。我会再次尝试在此处启用 server.logging
来调试您的测试。
此外,在针对模拟服务器编写 assert
s 时,在测试开始时定义路由处理程序,如 the example from the docs.
所示
根据 Sam 的建议,我能够通过第二次测试。我的困惑是如何针对我必须访问并执行操作的路由的请求参数进行断言。我不得不访问 /athletes
,单击不同的按钮,并且这些操作中的每一个都向 /athletes 路由发送单独的请求(和参数)。这就是为什么我试图在 andThen 块中重新定义路由处理程序(即在我已经使用 mirage/config 文件中的路由定义访问了路由之后)。
不喜欢我的解决方案,但我处理它的方式是将我的断言移出路由处理程序,而是将请求的值分配给 top-level 变量。这样,在我最后的 andThen() 块中,我能够断言对 /athletes 路由的最后一次调用。
assert.expect(1);
//will get assigned the value of 'request' on each server call
let athletesRequest;
//override server response defined in mirage/config in order to
//capture and assert against request/response after user actions
server.get('athletes', (db, request) => {
let athletes = db.athletes || [];
athletesRequest = request;
return {
athletes: athletes,
meta: { count: athletes.length }
};
});
//sends request to /athletes
visit('/athletes');
andThen(() => {
//sends request to /athletes
fillIn('.search-inner input', "ab");
andThen(function() {
//sends (final) request to /athletes
click('button.search');
andThen(function() {
//asserts against /athletes request made on click('button.search') assert.notOk(athletesRequest.queryParams.hasOwnProperty("contact"));
});
});
});
我仍然收到与 meta is not an object
相关的控制台错误,但它们并没有阻止测试通过。使用 server.logging = true
让我看到 meta 确实是所有 FakeServer 响应中的一个对象。
再次感谢山姆的建议。 server.logging = true
和 pauseTest()
使验收测试更容易进行故障排除。
遇到了几次失败,想知道我是否正确理解了 Mirage:
1.In ember-cli-mirage,我是否正确认为我定义的服务器响应应该反映我的实际服务器返回的内容?例如:
this.get('/athletes', function(db, request) {
let athletes = db.athletes || [];
return {
athletes: athletes,
meta: { count: athletes.length }
}
});
我正在使用自定义序列化程序,上面的内容与我的服务器响应此路由上的 get 请求的格式相匹配,但是,在两次测试中我遇到了两次失败并出现此错误:normalizeResponse must return a valid JSON API document: meta must be an object
2.Is mirage 强制执行 json:api 格式,这样做是因为我设置测试的方式吗?
例如,我有几个访问上述 /athletes
路由的测试,但是当我使用如下所示的异步调用时,我的失败发生了。我很想知道正确覆盖服务器响应行为的适当方法,以及为什么 normalizeResponse 错误出现在控制台中进行 2 次测试,但只会导致下面的测试失败。
test('contact params not sent with request after clicking .showglobal', function(assert) {
assert.expect(2);
let done = assert.async();
server.createList('athlete', 10);
//perform a search, which shows all 10 athletes
visit('/athletes');
fillIn('.search-inner input', "c");
andThen(() => {
server.get('/athletes', (db, request) => {
assert.notOk(params.hasOwnProperty("contacts"));
done();
});
//get global athletes, which I thought would now be intercepted by the server.get call defined within the andThen block
click('button.showglobal');
});
});
结果:
✘ Error: Assertion Failed: normalizeResponse must return a valid JSON API document:
* meta must be an object
expected true
我尝试按照上一个示例 here 中的建议将我的服务器响应更改为 json:api 格式,但这看起来与我的实际服务器响应完全不同,并导致我的测试失败,因为我的应用程序没有t 解析具有此结构的有效负载。必须感谢任何提示或建议。
你是对的。您上面显示的模拟是否发生故障?在我看来,它总是 return
meta
作为一个对象,因此在发出请求后通过查看控制台来验证响应是否符合您的想法。如果您想在测试期间看到回复,请在测试中输入
server.logging = true
:test('I can view the photos', function() { server.logging = true; server.createList('photo', 10); visit('/'); andThen(function() { equal( find('img').length, 10 ); }); });
不,Mirage 不了解您的特定后端,尽管它确实带有一些默认设置。我会再次尝试在此处启用
server.logging
来调试您的测试。此外,在针对模拟服务器编写
assert
s 时,在测试开始时定义路由处理程序,如 the example from the docs. 所示
根据 Sam 的建议,我能够通过第二次测试。我的困惑是如何针对我必须访问并执行操作的路由的请求参数进行断言。我不得不访问 /athletes
,单击不同的按钮,并且这些操作中的每一个都向 /athletes 路由发送单独的请求(和参数)。这就是为什么我试图在 andThen 块中重新定义路由处理程序(即在我已经使用 mirage/config 文件中的路由定义访问了路由之后)。
不喜欢我的解决方案,但我处理它的方式是将我的断言移出路由处理程序,而是将请求的值分配给 top-level 变量。这样,在我最后的 andThen() 块中,我能够断言对 /athletes 路由的最后一次调用。
assert.expect(1);
//will get assigned the value of 'request' on each server call
let athletesRequest;
//override server response defined in mirage/config in order to
//capture and assert against request/response after user actions
server.get('athletes', (db, request) => {
let athletes = db.athletes || [];
athletesRequest = request;
return {
athletes: athletes,
meta: { count: athletes.length }
};
});
//sends request to /athletes
visit('/athletes');
andThen(() => {
//sends request to /athletes
fillIn('.search-inner input', "ab");
andThen(function() {
//sends (final) request to /athletes
click('button.search');
andThen(function() {
//asserts against /athletes request made on click('button.search') assert.notOk(athletesRequest.queryParams.hasOwnProperty("contact"));
});
});
});
我仍然收到与 meta is not an object
相关的控制台错误,但它们并没有阻止测试通过。使用 server.logging = true
让我看到 meta 确实是所有 FakeServer 响应中的一个对象。
再次感谢山姆的建议。 server.logging = true
和 pauseTest()
使验收测试更容易进行故障排除。