简体   繁体   English

Ember-CLI-Mirage强制执行JSON:API?

[英]Ember-CLI-Mirage enforcing JSON:API?

Stumped on a couple failures and want to know if I'm understanding Mirage correctly: 遇到了几次失败,想知道我是否正确理解了Mirage:

1.In ember-cli-mirage, am I correct that the server response I define should reflect what my actual server is returning? 1.在ember-cli-mirage中,我是否纠正我定义的服务器响应应反映我的实际服务器返回的内容? For example: 例如:

this.get('/athletes', function(db, request) {
  let athletes = db.athletes || [];
  return {
    athletes: athletes,
    meta: { count: athletes.length }
  }
});

I am using custom serializers and the above matches the format of my server response for a get request on this route, however, on two tests I'm getting two failures with this error: normalizeResponse must return a valid JSON API document: meta must be an object 我正在使用自定义序列化程序,并且上面的方法与此服务器上针对此路由的get请求的服务器响应格式相匹配,但是,在两次测试中,我因此错误而遇到两次失败: normalizeResponse must return a valid JSON API document: meta must be an object

2.Is mirage enforcing the json:api format, and is it doing so because of the way I'm setting up the tests? 2.Mirage是否正在执行json:api格式,是否由于我设置测试的方式而这样做?

For example, I have several tests that visit the above /athletes route, yet my failures occur when I use an async call like below. 例如,我有几个测试可以访问上面的/athletes路由,但是当我使用如下所示的异步调用时,就会发生失败。 I would love to know the appropriate way to correctly overwrite the server response behavior, as well as why the normalizeResponse error appears in the console for 2 tests but only causes the one below to fail. 我很想知道正确覆盖服务器响应行为的适当方法,以及为什么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');
  });
});

Result: 结果:

✘ Error: Assertion Failed: normalizeResponse must return a valid JSON API document:
    * meta must be an object
         expected true

I tried changing my server response to a json:api format as suggested in the last example here but this looks nothing like my actual server response and causes my tests to fail since my app doesn't parse a payload with this structure. 我试图改变我的服务器响应JSON:API格式在最后一个例子建议在这里 ,但这个看上去一点也不像我的实际服务器响应,并导致我的测试失败,因为我的应用程序不分析这个结构的有效载荷。 Any tips or advice must appreciated. 任何提示或建议都必须感谢。

  1. You are correct. 你是对的。 Are the failures happening for the mock you've shown above? 您上面显示的模拟程序是否正在发生故障? It looks to me like that would always return meta as an object, so verify the response is what you think it should be by looking in the console after the request is made. 在我看来,这总是将meta作为对象返回,因此在发出请求后,通过在控制台中查看来验证响应是否符合您的想法。

    If you'd like to see responses during a test, enter server.logging = true in your test: 如果您想在测试过程中看到响应,请在测试中输入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 ); }); }); 
  2. No, Mirage is agnostic about your particular backend, though it does come with some defaults. 不,Mirage不了解您的特定后端,尽管它确实带有一些默认设置。 Again I would try enabling server.logging here to debug your tests. 同样,我会尝试在此处启用server.logging来调试您的测试。

    Also, when writing assert s against the mock server, define the route handlers at the beginning of the test, as shown in the example from the docs . 同样,在针对模拟服务器编写assert时,请在测试开始时定义路由处理程序,如docs的示例所示。

I was able to get my second test to pass based on Sam's advice. 根据Sam的建议,我能够通过第二次考试。 My confusion was how to assert against the request params for a route that I have to visit and perform actions on. 我的困惑是如何针对我必须访问并对其执行操作的路线的请求参数进行断言。 I was having to visit /athletes , click on different buttons, and each of these actions was sending separate requests (and params) to the /athletes route. 我不得不访问/athletes ,单击不同的按钮,并且每个动作都向/ athletes路由发送了单独的请求(和参数)。 That's is why I was trying to redefine the route handler within the andThen block (ie after I had already visited the route using the route definition in my mirage/config file). 这就是为什么我试图在andThen块内重新定义路由处理程序的原因(即在我已经使用Mirage / config文件中的路由定义访问了路由之后)。

Not in love with my solution, but the way I handled it was to move my assertion out of route handler and instead assign the value of the request to a top-level variable. 我不喜欢我的解决方案,但是我处理它的方式是将断言移出路由处理程序,而是将请求的值分配给顶级变量。 That way, in my final andThen() block, I was able to assert against the last call to the /athletes route. 这样,在我的最终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"));
      });
    });
  });

I'm still getting console errors related to meta is not an object , but they are not preventing tests from passing. 我仍然收到与meta is not an object相关的控制台错误meta is not an object ,但是它们并不能阻止测试通过。 Using the server.logging = true allowed me to see that meta is indeed an object in all FakeServer responses. 使用server.logging = true可以使我看到meta确实是所有FakeServer响应中的对象。

Thanks again to Sam for the advice. 再次感谢山姆的建议。 server.logging = true and pauseTest() make acceptance tests a lot easier to troubleshoot. server.logging = truepauseTest()使验收测试更容易排错。

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

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