簡體   English   中英

Ember-CLI-Mirage強制執行JSON:API?

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

遇到了幾次失敗,想知道我是否正確理解了Mirage:

1.在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.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

我試圖改變我的服務器響應JSON:API格式在最后一個例子建議在這里 ,但這個看上去一點也不像我的實際服務器響應,並導致我的測試失敗,因為我的應用程序不分析這個結構的有效載荷。 任何提示或建議都必須感謝。

  1. 你是對的。 您上面顯示的模擬程序是否正在發生故障? 在我看來,這總是將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 ); }); }); 
  2. 不,Mirage不了解您的特定后端,盡管它確實帶有一些默認設置。 同樣,我會嘗試在此處啟用server.logging來調試您的測試。

    同樣,在針對模擬服務器編寫assert時,請在測試開始時定義路由處理程序,如docs的示例所示。

根據Sam的建議,我能夠通過第二次考試。 我的困惑是如何針對我必須訪問並對其執行操作的路線的請求參數進行斷言。 我不得不訪問/athletes ,單擊不同的按鈕,並且每個動作都向/ athletes路由發送了單獨的請求(和參數)。 這就是為什么我試圖在andThen塊內重新定義路由處理程序的原因(即在我已經使用Mirage / config文件中的路由定義訪問了路由之后)。

我不喜歡我的解決方案,但是我處理它的方式是將斷言移出路由處理程序,而是將請求的值分配給頂級變量。 這樣,在我的最終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相關的控制台錯誤meta is not an object ,但是它們並不能阻止測試通過。 使用server.logging = true可以使我看到meta確實是所有FakeServer響應中的對象。

再次感謝山姆的建議。 server.logging = truepauseTest()使驗收測試更容易排錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM