2014-04-25 3 views
2

서비스에 대해 실행되는 일부 테스트 스크립트에서 타이밍 문제가 발생합니다. Restrict JSON 클라이언트를 사용하여 약속보다는 콜백을 사용하는 호출을 발행합니다. 완성 된 함수를 내 GivenWhen으로 전달 했으므로 이러한 비동기 호출에 대해 필요한 블로킹을 수행 할 수 있으므로 일관성없는 테스트 스위트 실행을 막을 수 있습니다 (dands가없는 경우, tossup 및 which가 얼마나 많은지 ThenAnd 초).테스트 Mocha-Cakes 및 Coffescript를 사용하여 서비스를 다시 설정하면 비동기에서 done()을 사용하면 시간 초과 문제가 발생합니다.

나는 보통 coffescript에 익숙하며 초보자 만 모카/모카 - 케이크에 관해서는, 그래서 나는 확실히 내 코드에서 뭔가 잘못하고있다. \ 개발 \ 클라이언트가 Pensco을 \ \ AngularJS와 :

C :

require 'mocha-cakes' 
should = require 'should' 
restify = require 'restify' 


Feature "Account API", 
    "In order to have control over structured Account documents", 
    "as a consumer of investment account information,", 
    "I need a RESTful service API.", -> 

    Scenario "GET /account/:userid", -> 

     client = restify.createJSONClient 
     url: "http://localhost:8080", 
     version: "*" 

     _e1 = null 
     _r1 = null 

     _e2 = null 
     _r2 = null 
     _d2 = null 

     # GET non-existent account 
     Given "I have not yet created the Account", -> 
     When "I request the Account", (done) -> 
     client.get "/account/99", (err, req, res, obj) -> 
      _e1 = err 
      _r1 = res 
      done() 
      err 

     Then "it should respond with an error", -> 
     _e1.should.be.ok 
     And "the status code should be 404", -> 
     _r1.should.have.status 404 


     # GET existent account 
     Given "I have created the Account", (done) -> 
     client.post "/account", { userId: 1, accountType: 0, accountCategories: [], beneficiaries: [], accountOwner: { firstName: "Test", lastName: "User" } }, (err) -> 
      done() 
      err 

     When "I request the Account", (done) -> 
     client.get "/account/1", (err, req, res, obj) -> 
      _e2 = err 
      _r2 = res 
      _d2 = obj 
      done() 
      err 

     Then "it should responond with a document", -> 
     _d2.should.be.ok 
     And "it should have the userId 1", -> 
     _d2.userId.should.eql 1 
     And "it should have an accountOwner property", -> 
     _d2.accountOwner.should.be.ok 
     And "the status code should be 200", -> 
     _r2.should.have.status 200 

내가 이것을 실행하면, 내 출력은 항상 다음과 같다 : 여기서 실패 테스트 케이스의 몇 가지의 예입니다 \ Pensco \ newaccountwizard.api> 모카 테스트/AccountAPITests.coffee -r 사양이 커피 --compilers -R한다 : 커피 스크립트/등록을

기능 : 계정 API

,142 1 실패 3 아직 647,333,210

6 전달 (6S)

1) 특징 : 계정 API

In order to have control over structured Account documents 
    as a consumer of investment account information, 
    I need a RESTful service API. 

Scenario: GET /account/:userid ◦ When: I request the Account: 
Error: timeout of 2000ms exceeded 
at [object Object].<anonymous> (C:\Users\Jon\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:139:19) 
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 

2) 특징 : 계정 API

In order to have control over structured Account documents 
    as a consumer of investment account information, 
    I need a RESTful service API. 

Scenario: GET /account/:userid ◦ Given: I have created the Account: 
Error: timeout of 2000ms exceeded 
at [object Object].<anonymous> (C:\Users\Jon\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:139:19) 
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 

3) 특징 : 계정 API

In order to have control over structured Account documents 
    as a consumer of investment account information, 
    I need a RESTful service API. 

Scenario: GET /account/:userid ◦ When: I request the Account: 
Error: timeout of 2000ms exceeded 
at [object Object].<anonymous> (C:\Users\Jon\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:139:19) 
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 

이제 client.get/client.post를 통한 REST 호출이 거의 즉시 발생한다는 것을 알았습니다. 내가 복원 서비스 서버를 다시 시작한 후 첫 번째 실행을 제외하고는 일반적으로 첫 번째 또는 두 번째 Then/And 만 실패하고 나머지는 성공적으로 완료됩니다. 아마도 몇 밀리 초의 타이밍 문제가 있지만 확실히 2000ms는 아닙니다. 내가 왜 Givens와 Whens가 갑자기 시간을 내기 시작하는지 궁금 하네. 내가 done() 전화를 걸면.

mocha-cakes가 coffescript Feature-> Scenario-> Given/When-> Then/And/...를 describe/it calls로 변환하는 방법을 오해하고 있습니다. 어떻게 든 적용 범위는 모카 - 케이크 스크립트 구조의 성격을 부여받는 것보다 큽니다 의심 스럽습니다 ... 그 범위가 정확히 무엇인지 정확하게 모르겠습니다.

답변

0

나는 또한 모카 케이크에 익숙하지 않습니다. 나는 mocha/(점화) 커피를 사용하여 복원을 테스트하고 있습니다. 최신 모카 (mocha)가 약속을 잘 알고 있기 때문에 약속을 지키는 것이 편리하다는 것을 알았습니다. 그렇다면 나는 "끝났다"고 귀찮게 생각하지 않아도됩니다. 또한 res.end() 또는 res를 호출해야 할 수도 있습니다.

Promise = require('bluebird') # or whatever, I presume 
port = 8776     # ditto 

getHttpJson = (addr)-> 
    addr = normalizeUrl(addr) 
    new Promise (resolve, reject)-> 
    req = http.get(addr, _completeResponse(resolve)) 
     .on('error', reject) 
    req.end() 

일반적인 경우 : 이력서() 그냥 "GET"를

(this explanation 참조) 구성 요소에

requestHttpJson = (method, addr, data)-> 
    if data? 
    data = JSON.stringify(data) 
    urlBits = getUrlBits(addr) 
    new Promise (resolve, reject)-> 
    req = http.request(
     method: method 
     headers: { 
     "Content-Type": "application/json" } 
     hostname: urlBits.hostname 
     port: urlBits.port 
     path: urlBits.pathname 
    , _completeResponse(resolve)) 
    req.on('error', reject) 
    if data? 
     req.write(data) 
    req.end() 

postHttpJson = (addr, data)-> 
    requestHttpJson('POST', addr, data) 
putHttpJson = (addr, data)-> 
    requestHttpJson('PUT', addr, data) 
deleteHttpJson = (addr, data)-> 
    requestHttpJson('DELETE', addr, data) 

브레이크 다운 주소 및 기본값을 추가합니다. ("포트는"글로벌 모듈입니다.) 요청 & 해결의 몸을 구문 분석하는

getUrlBits = (addr)-> 
    bits = url.parse(addr) 
    bits.port = bits.port || port 
    bits.hostname = bits.hostname || 'localhost' 
    bits.protocol = bits.protocol || 'http' 
    return bits 

normalizeUrl = (addr)-> 
    url.format(getUrlBits(addr)) 

유틸리티.

_completeResponse = (resolve)-> 
    (res)-> 
    body = [] 
    res.on 'data', (data)-> 
     body.push data 
    res.on 'end', -> 
     body = body.join '' 
     content = if body == '' then null else JSON.parse(body) 
     resolve([res,content])