大家好,
我有一个案例,我无法在步骤定义中使用 async。
我有一个简单的场景,我通过我的应用程序的 api 接口登录以获取身份验证令牌
我的特征文件是:
Feature: Login
Scenario: Login using api
Given I login to my account with my username and password
Then I should get an authorization token
|username|password|
|[email protected]|dev|
我的步骤是:
defineSupportCode(({ Given, Then, setDefaultTimeout }) => {
const timeOut = 30000;
const baseSteps: BaseSteps = new BaseSteps();
setDefaultTimeout(timeOut);
// tslint:disable-next-line:only-arrow-functions
Given(/^I login to my account with my username and password$/, async (table: TableDefinition) => {
const userData = table.hashes();
const loginResponse = await baseSteps.loginUser(userData[0].username, userData[0].password);
console.log('Login response is ', loginResponse);
const statusCode = 302;
expect(await loginResponse.status).to.equal(statusCode);
});
Then(/^I should get an authorization token$/, async () => {
const authorizationHeader = await baseSteps.getAuthorizationHeader();
console.log('Auth Header', authorizationHeader);
expect(authorizationHeader).to.not.equal(null);
const orders = await fetch('url',
{
method: 'GET', headers: {
authorization: authorizationHeader
}
});
// tslint:disable-next-line:no-console
console.log(await orders.json());
});
});
我在 BaseSteps.ts 中的辅助函数如下:
async loginUser(userName: string, password: string): Promise<Response> {
const getSignInFormResponse = await fetch(this.getInitialRequestUrl(),
{
method: 'GET'
});
const form = <some-form-data>;
const loginResponse = await fetch(getSignInFormResponse.url,
{
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
body: form,
redirect: 'manual'
});
return loginResponse;
}
async getAuthorizationHeader() {
const tokenResponse = await fetch(this.getInitialRequestUrl(),
{
method: 'GET',
redirect: 'manual'
});
const tokenInfo = qs.parse(tokenResponse.headers.get('location'));
const authorizationHeader = `${tokenInfo.token_type} ${tokenInfo.access_token}`;
return authorizationHeader;
}
当我运行我的场景时,它失败并出现以下错误:
Error: function uses multiple asynchronous interfaces: callback and promise
在我的 Given 开始的那一行
请帮我修复这个错误。
您的Given
步骤定义有一个table
参数,而在您的功能文件中,它没有表格。 Cucumber-js 根据步骤定义中的参数数量确定您是否使用callback
接口。 如果该数字比传入的参数数量多 1,则假定您正在使用回调接口。 由于您还返回了一个承诺,因此它确定您正在使用承诺接口。 一次只能使用一个接口。
关于更新错误消息以包含关于假设回调接口是因为步骤定义具有 X 个参数的想法的想法?
@charlierudolph嘿,伙计,感谢您的回复,但我不太明白您为什么说我的功能文件中没有表格?
Given I login to my account with my username and password
Then I should get an authorization token
|username|password|
|[email protected]|dev|
我们不是如何在特征文件中传递表格的?
从这里获得灵感https://github.com/cucumber/cucumber-js/blob/master/features/data_tables.feature
是的,错误信息可能会更清楚一点。
再次感谢。
我的意思是在您的功能文件中,您的Then
步骤有一个表格,但您的Given
步骤没有。
呃该死的.. 是的,你是对的.. 我把桌子误认为是example
抱歉造成混乱。 我想现在应该没问题了。 如果问题仍然存在,我会回来再次提问。
以@charlierudolph 的身份结束问题帮助我找出了我自己在功能文件中的编码错误。
很高兴它为你工作。 我将打开一个单独的问题来更新错误消息,希望以后更容易弄清楚
由于关闭后没有任何近期活动,因此该线程已自动锁定。 请为相关错误打开一个新问题。
最有用的评论
您的
Given
步骤定义有一个table
参数,而在您的功能文件中,它没有表格。 Cucumber-js 根据步骤定义中的参数数量确定您是否使用callback
接口。 如果该数字比传入的参数数量多 1,则假定您正在使用回调接口。 由于您还返回了一个承诺,因此它确定您正在使用承诺接口。 一次只能使用一个接口。关于更新错误消息以包含关于假设回调接口是因为步骤定义具有 X 个参数的想法的想法?