0
私は現在のプロジェクトでいくつかの状況に遭遇しましたが、私はどのように対処するのか分からないと約束しています。ここで連鎖約束のテスト(ジャスミン、リアクション、カルマ)
は、関連するコード・ブロックです:
return this.axios.get(path, requestOpts)
.then((response) => {console.log('did authorize: ', response); return response})
.then((response) => {
if (response.data.ok) {
window.localStorage.setItem(path, JSON.stringify(response.data));
console.log("Setting localStorage item ", path, response.data);
return response.data.payloadUrl;
} else {
console.error("Non-ok response for ", path, response.data);
const resp: DisplayTokenResponse = response.data;
//TODO: reject promise?
if (resp.status === "AUTHENTICATION_REQUIRED") {
this.axiosService.goToLoginPage(window.location + '');
}
Promise.reject(response.data.message);
}
});
私のテスト(これまでに)次のようになります。
describe('.authorize()',() => {
let axiosSpy: jasmine.Spy;
beforeEach((done) => {
spyOn(svc, 'keyPath').and.returnValue(path);
spyOn(svc, 'storedToken').and.returnValue(stored);
let response = {
data: {
ok: true,
message: 'test-response',
payloadUrl: 'http://payload-url.com'
}
}
spyOn(svc.axios, 'get').and.callFake(
(path:string, reqOpts:AxiosRequestConfig) => {
return new Promise(() => {
response
});
}, (e) => {
console.log(`failed`);
});
});
describe('should authorize user',() => {
it('when supplied a STRING',() => {
clientId = clientId_string;
});
it('when supplied a NUMBER',() => {
clientId = clientId_number;
});
afterEach((done) => {
svc.authorize(clientId, locationId, screenId).then((result) => {
console.log(`result ${result}`);
done();
}, (e) => {
console.log(`failed with error ${e}`);
done();
});
});
});
});
私は1レベルダウンの約束をテストすることができますが、どのように私は設定しますこのような状況に対処できるように私のテストをアップしますか?
「状況」もあなたも参考にしてください。また、外部からの拒否を目立たせるには、 'then'ブロックからPromiseを返す必要があります。 - > ** return ** Promise.reject(response.data.message); ' –
Promiseは、このような複数の '' .then''ステートメントによって連鎖されます。私はそれが各ステップで解決することを確認しようとしています。 '' 'this.axios.get'''からの最初の約束からの応答を模擬しようとすると、テストは何も返さずにタイムアウトします。ログに基づいて、それは第2の '' .then''の受け入れ/拒否段階には決して見えません。 –