2017-10-11 110 views
1

我試圖斷言代碼中的幾個步驟(訪問頁面,提供錯誤的卡號,密碼組合和單擊提交)應該從後端服務產生錯誤 - 我提到this已經..Qunit斷言拋出不起作用

並嘗試使用錯誤對象作爲assert.throws的第二個參數的建議,但這對我不起作用。

我沒有看到this鏈接以及發佈我的問題之前, 我的問題是 - 我沒有控制在這種情況下引發異常/錯誤的代碼。 (我不能改變它說Ember.assert等)我只是想能夠捕捉到一個錯誤的情況。

其次,在這種情況下我沒有組件。它是一個直接的API調用,當點擊提交完成後,基本上會在控制器中調用一個操作submitAuthForm,該調用將調用ember cli mirage場景,該場景返回以下對象問題對象。

return new Response(401,{'X-Auth-Token': 'wrong-username-password-combination'},failResponse401); 

和返回的對象看起來像

var failResponse401 = { 
       problems: [ { 
        field: null, 
        index: null, 
        value: null, 
        code: '0006', 
        subCode: '', 
        details: {}, 
        _type: 'error' 
       } ] 
      }; 

我們有一個內部的例外套件,拋出基於此錯誤對象node_module依賴。

這裏是我的Qunit測試

test('ERROR_CASE_visiting /signon, provide cardNumber(2342314) and ' + 
'password, submit and expect to see invalid cardnumber/password error', 
function (assert) { 

assert.expect(2); 
assert.throws(
function() { 
    visit('/signon'); 
    fillIn('#a8n-signon-card-number input', '2342314'); 
    fillIn('#a8n-signon-password input', 'password'); 
    click('#a8n-signon-submit-button button'); 
}, 
Error, 
"Error Thrown" 
); 
}); 

我一直從Qunit

Error [email protected] 110 ms 
Expected: 
function Error(a){ 
    [code] 
} 
Result:  
undefined 
Diff: 
function Error(a){ 
    [code] 
}defined 
Source:  
    at Object.<anonymous> (http://localhost:7357/assets/tests.js:175:12) 
    at runTest (http://localhost:7357/assets/test-support.js:3884:30) 
    at Test.run (http://localhost:7357/assets/test-support.js:3870:6) 
    at http://localhost:7357/assets/test-support.js:4076:12 
    at Object.advance (http://localhost:7357/assets/test-support.js:3529:26) 
    at begin (http://localhost:7357/assets/test-support.js:5341:20) 
API rejected the request because of : []@ 1634 ms 
Expected: 
true 
Result:  
false 
Source:  
Error: API rejected the request because of : [] 
    at Class.init (http://localhost:7357/assets/vendor.js:172237:14) 
    at Class.superWrapper [as init] (http://localhost:7357/assets/vendor.js:55946:22) 
    at new Class (http://localhost:7357/assets/vendor.js:51657:19) 
    at Function._ClassMixinProps.create (http://localhost:7357/assets/vendor.js:51849:12) 
    at Function.createException (http://localhost:7357/assets/vendor.js:172664:16) 
    at Class.<anonymous> (http://localhost:7357/assets/vendor.js:133592:72) 
    at ComputedPropertyPrototype.get (http://localhost:7357/assets/vendor.js:32450:27) 
    at Object.get (http://localhost:7357/assets/vendor.js:37456:19) 
    at Class.get (http://localhost:7357/assets/vendor.js:50194:26) 
    at http://localhost:7357/assets/vendor.js:133645:30 

收到此錯誤還有什麼,我可以嘗試得到它的工作。 有沒有辦法以某種方式通過這個測試,通過以某種方式包裝返回的響應,使得它不會中斷我的測試。

+1

在這個問題的討論可能是有用的https://stackoverflow.com/questions/42781085/ember-render-hbs-swallowing-thrown-error –

+0

我也看到該鏈接,以及發佈前,我已經更新我的問題上面考慮鏈接提供的信息。基本上我的問題不在於測試組件,而且更改異常拋出代碼的源代碼不在我的控制範圍之內 –

回答

0

我發現下面這個link

用戶pablobm一種解決方法發佈了一個link給助手 我用,以解決此問題Qunit。