2015-12-29 25 views
2

難倒了幾個失誤,想知道如果我理解正確幻影:Ember-CLI-Mirage實施JSON:API?

1.In燼-CLI-海市蜃樓,我是正確的,因爲服務器的響應,我定義應反映我的真實服務器返回?例如:

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.海市蜃樓強制執行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格式在最後一個例子here建議,但這個看上去一點也不像我的實際服務器響應,並導致我的測試失敗因爲我的應用不會使用這種結構來分析有效載荷。任何提示或建議,必須讚賞。

回答

4
  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. 沒有,幻影是不可知的關於你的特別的後臺,雖然它帶有一些默認值。我再次嘗試在這裏啓用server.logging來調試您的測試。

    此外,在寫模擬服務器的assert時,在測試開始時定義路由處理程序,如the example from the docs所示。

+1

嗨,山姆,我能夠解決我的失敗,使用您的建議將模擬服務器移動到測試頂端。我已在下面發佈我的答案。再次感謝您的快速反應(和Mirage!)。 – Jim

0

我能夠根據Sam的建議讓我的第二個測試通過。我的困惑是,如何針對我必須訪問並執行操作的請求參數提出申訴。我不得不訪問/athletes,點擊不同的按鈕,並且這些操作中的每一個都向/運動員路線發送單獨的請求(和參數)。這就是爲什麼我試圖在andThen塊內重新定義路由處理程序(即,在我已經使用我的mirage/config文件中的路由定義訪問了路由之後)。

不愛我的解決方案,但我處理這是將我的說法了路由處理的,而是分配請求頂級變量的值的方式。這樣,在我的最後和()()塊中,我能夠堅持反對最後一次對/運動員路線的呼叫。

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使我能夠看到元實際上是所有FakeServer響應中的一個對象。

再次感謝山姆的意見。 server.logging = truepauseTest()使驗收測試更容易排除故障。