2017-02-06 6 views
0

ノード7.4.0// 4.14.0/typescriptです2.1.5/m個の3..1.2 /チャイ3.5.0を表現チャイ-HTTP 3.0.0ノード/ ExpressのAPIテストが見つかりませんでした失敗

私がしようとしています

0:リソースをテストするために、モカ/チャイを使用しますが、アプリケーションを実行すると、404エラー

HeroRouter.ts

.... 
    /** 
    * GET one hero by id 
    */ 
    public getOne(req: Request, res: Response, next: NextFunction) { 
     let query = parseInt(req.params.id); 
     let hero = Heroes.find(hero => hero.id === query); 
     if (hero) { 
      res.status(200) 
       .send({ 
        message: 'Success', 
        status: res.status, 
        hero 
       }); 
     } 
     else { 
      res.status(404) 
       .send({ 
        message: 'No hero found with the given id.', 
        status: res.status 
       }); 
     } 
    } 

実行中は、次のログが表示されます表示されたら、それは失敗していますが見つかりません

BUT試験は失敗し書き込もう:

herotest.ts 記述( 'GET API/V1 /ヒーロー/:ID'、()=> {

it('should respond with Not Found',() => { 
     return chai.request(app).get('/api/v1/heroes/-32') 
      .then(res => { 
       expect(res.status).to.equal(404); 
      }); 
    }); 

}); 

NPM

GET /api/v1/heroes/-32 404 1.209 ms - 46 
    1) should respond with Not Found 

    1 failing 

    1) GET api/v1/heroes/:id should respond with Not Found: 
    Error: Not Found 
     at Test.Request.callback (node_modules/superagent/lib/node/index.js:626:17) 
     at node_modules/superagent/lib/node/index.js:795:18 
     at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/parsers/json.js:16:7) 
     at endReadableNT (_stream_readable.js:974:12) 
     at _combinedTickCallback (internal/process/next_tick.js:74:11) 
     at process._tickCallback (internal/process/next_tick.js:98:9) 

に失敗テストがを解決しました。セルゲイの答えに従って - ステータスコードとして

it('should respond with Not Found',() => { 
    return chai.request(app).get('/api/v1/heroes/-32') 
     .catch(function (res) { 
      expect(res.status).to.equal(404); 
     }); 
}); 

答えて

2

要求がsuperagent(ここで、スタックトレースポイントに)、成功しなかったことを示すエラーのようにこれを扱います:source。あなたのthenブランチの約束は決して実行されず、あなたのコードによって約束された拒否が放棄されます。

この動作は、chai-httpsuperagentの両方で修正されましたが、まだ元のためにNPMにリリースされていないように見える。それを明確にするためのhttps://github.com/chaijs/chai-http/issues/75

+0

感謝を... – erwin

+0

@erwinは私が関連へのリンクをちょうどを追加しましたギブバブ問題 –

関連する問題