2016-10-17 62 views
1

我正在嘗試使用摩卡,柴和sinon的反應組件進行單元測試。假服務器響應正文應該是字符串,但未定義

我的第一個測試工作,它是簡單的檢查組件是否存在,如果道具使用正確。

現在我在測試中遇到了麻煩。

這裏是我的代碼:

import * as React from 'react'; 
import chai from 'chai'; 
import TestUtils from 'react-addons-test-utils'; 
import TransmissorAdd from '../../../middle/transmissors/TransmissorAdd'; 
import sinon from 'sinon' 

const expect = chai.expect 

describe('components/transmissors/TransmissorAdd',() => { 

    let params = { 
     method: 'add' 
    } 
    var server = null; 

    beforeEach(function() { 
     server = sinon.fakeServer.create(); 
    }); 

    afterEach(function() { 
     server.restore(); 
    }); 

    it('ajax working',() => { 
     // Set up the fake response 
     server.respondWith('GET', '/api/client/1/', 
      [200, {'Content-Type': 'application/json'}, 
       JSON.stringify(
        { 
         "id": 1, 
         "first_name": "firstname", 
         "last_name": "lasname", 
         "account": "0016", 
         "cpf": "55555555555", 
         "rg": "5555555555", 
         "birthdate": "0000-00-00", 
         "street": "Av. street", 
         "number": 881, 
         "complement": "", 
         "district": "xxxxxx", 
         "city": "city", 
         "country": "Brasil", 
         "state": "RS", 
         "zip_code": "00000000", 
         "health_plan": "", 
         "account_phone": "5599999999", 
         "contact_phone": "", 
         "key_box": "", 
         "general_info": "" 
        } 
       ) 
      ] 
     ); 

     server.respondWith('POST', '/api/transmissors/', 
      [200, JSON.stringify({'response': 'ok'})]); 


     const transmissorAdd = TestUtils.renderIntoDocument(
      <TransmissorAdd params={params} /> 
     ) 

     server.respond(); 
    }) 

}); 

我收到這個錯誤消息:

TypeError: Fake server response body should be string, but was undefined 
     at responseArray (node_modules/sinon/lib/sinon/util/fake_server.js:31:19) 
     at Object.respondWith (node_modules/sinon/lib/sinon/util/fake_server.js:178:67) 
     at Context.<anonymous> (assets/js/components/__tests__/middle/transmissors/TransmissorAdd.test.js:55:16) 

有什麼不對?

在此先感謝

+0

貴組件調用/ API? – devside

+0

是的,我所有的組件都調用/ api –

回答

1

看起來你需要傳遞空選項{}

server.respondWith('POST', '/api/transmissors/', [200, {}, JSON.stringify({'response': 'ok'})]); 

你可以使用這個簡短形式

server.respondWith('POST', '/api/transmissors/', JSON.stringify({'response': 'ok'})); 
相關問題