Typescript Error: Argument of type 'number' is not assignable to parameter of type 'Expected<Promise<number>>'
Typescript Error: Argument of type 'number' is not assignable to parameter of type 'Expected<Promise<number>>'
我有以下函数执行失败:
it('should show three items', () => {
const EXPECTED_NUMBER_OF_ITEMS: number = 3;
const countOfTabElements: Promise<number> = page.listOfTabs.count();
expect(countOfTabElements).toBe(EXPECTED_NUMBER_OF_ITEMS);
});
当我执行它时抛出以下错误:
Argument of type 'number' is not assignable to parameter of type 'Expected>'. (2345)
知道为什么吗?
尝试:
it('should show three items', () => {
const EXPECTED_NUMBER_OF_ITEMS: number = 3;
page.listOfTabs.count().then(value => {
expect(value).toBe(EXPECTED_NUMBER_OF_ITEMS);
});
});
作为@Nitzan Tomer 的替代答案,您应该可以使用async/await(TS >= 2.1 用于定位 ES5)
it('should show three items', async () => {
const EXPECTED_NUMBER_OF_ITEMS: number = 3;
const value = await page.listOfTabs.count();
expect(value).toBe(EXPECTED_NUMBER_OF_ITEMS);
});
(作为旁注,我相信您需要相当新版本的 Mocha 才能正确处理承诺拒绝)
我会说使用 async 和 await 以最少的代码更改来处理 promise 对象。
// no changes to page.po.ts. handle async and await in .spec.ts file
import {browser, by, element} from 'protractor';
export class HomePage {
navigateTo() {
return browser.get('/');
}
getParagraphText() {
return element(by.css('cfs-root h1')).getText();
}
}
//page.e2e-spec.ts
import {HomePage} from './home.po';
describe('ng-app App', () => {
let page: HomePage;
beforeEach(() => {
page = new HomePage();
});
it('should display the page heading.', async () => { // notice the callback is a async function
page.navigateTo();
expect(await page.getParagraphText()).toBe('cfs works!'); //here we are using await to handle the promise
page.getHomeSearchHeading().then((text) => {
expect(text).toEqual('Universal Data Catalog');
});
});
});
我有以下函数执行失败:
it('should show three items', () => {
const EXPECTED_NUMBER_OF_ITEMS: number = 3;
const countOfTabElements: Promise<number> = page.listOfTabs.count();
expect(countOfTabElements).toBe(EXPECTED_NUMBER_OF_ITEMS);
});
当我执行它时抛出以下错误:
Argument of type 'number' is not assignable to parameter of type 'Expected>'. (2345)
知道为什么吗?
尝试:
it('should show three items', () => {
const EXPECTED_NUMBER_OF_ITEMS: number = 3;
page.listOfTabs.count().then(value => {
expect(value).toBe(EXPECTED_NUMBER_OF_ITEMS);
});
});
作为@Nitzan Tomer 的替代答案,您应该可以使用async/await(TS >= 2.1 用于定位 ES5)
it('should show three items', async () => {
const EXPECTED_NUMBER_OF_ITEMS: number = 3;
const value = await page.listOfTabs.count();
expect(value).toBe(EXPECTED_NUMBER_OF_ITEMS);
});
(作为旁注,我相信您需要相当新版本的 Mocha 才能正确处理承诺拒绝)
我会说使用 async 和 await 以最少的代码更改来处理 promise 对象。
// no changes to page.po.ts. handle async and await in .spec.ts file
import {browser, by, element} from 'protractor';
export class HomePage {
navigateTo() {
return browser.get('/');
}
getParagraphText() {
return element(by.css('cfs-root h1')).getText();
}
}
//page.e2e-spec.ts
import {HomePage} from './home.po';
describe('ng-app App', () => {
let page: HomePage;
beforeEach(() => {
page = new HomePage();
});
it('should display the page heading.', async () => { // notice the callback is a async function
page.navigateTo();
expect(await page.getParagraphText()).toBe('cfs works!'); //here we are using await to handle the promise
page.getHomeSearchHeading().then((text) => {
expect(text).toEqual('Universal Data Catalog');
});
});
});