2017-01-19 12 views
0

私はapiを呼び出して、特定の応答値(id)を変数に保存して、さらなるテストケースで同じIDを使用できるようにします。mochaの変数にapiレスポンスを保存する方法は?

it('1: Valid userId', function (done) { 
    servicesGenerator.getPlayoApi(apiEndPoints.getValidFetchPlaypalsApi()) 
     .end(function (err, res) { 
      baseValidator(err, res, 1, responseMsg.fetchPlaypalsSuceess); 
      done(); 
     }); 
}); 

レスポンスボディから目的の値を抽出してit()の外部で使用するにはどうすればよいですか。

私は

var palId; 
it('1: Valid userId', function (done) { 
servicesGenerator.getPlayoApi(apiEndPoints.getValidFetchPlaypalsApi()) 
    .end(function (err, res) { 
     palId=res.body.pal[0].palId 
     baseValidator(err, res, 1, responseMsg.fetchPlaypalsSuceess); 
     done(); 
    }); 

}のような何かをしたいです)。

このpalIdはコードのどこにでも使用できます。

答えて

0

あなたは変数を宣言し、それをそのように使用することができます。

describe('test suite', function() { 
    let component; 

    beforeEach(function() { 
    return asyncFunction(); 
    }); 

    it('test',() => { 
    // use variable here 
    }); 
}); 
+0

私は()内から変数の値を取得し、それ()ブロックの外側にそれを使用したいが。 –

+0

コンポーネント変数はit()のすべてで使用できるので、それを変更すると他のものや外部スコープでも使用できます –

関連する問題