2016-10-17 18 views
1

mocha、chai、sinonと反応するコンポーネントで単体テストを実行しようとしています。偽のサーバレスポンスボディは文字列である必要がありますが、定義されていません

私の最初のテストは機能しました。コンポーネントが存在し、小道具が正しく使用されているかどうかを確認するのは簡単です。

今、私はテストでajaxに問題があります。

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) 

何が問題になっています:私は、エラーのこのメッセージが表示さ

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(); 
    }) 

}); 

:ここ

は私のコードですか?それはあなたが空にオプションを渡す必要があるように見える進歩

+0

を使用することができますか? – devside

+0

はい、すべてのコンポーネントが/ apiを呼び出します –

答えて

1

おかげで、{}

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

あなたは、あなたのコンポーネントが/ APIを呼び出しています。この短い形式

server.respondWith('POST', '/api/transmissors/', JSON.stringify({'response': 'ok'})); 
関連する問題