即使我尝试为下面的步骤定义片段定义它,也无法读取js中数组的未定义属性(读取“push”)

问题描述 投票:0回答:1

我正在使用以下步骤定义:

Given the notification was successfully sent to the customer "<customer>"
  | result       | customer  |
  | successfully | customer1 |
  | successfully | customer2 |

下面是它的实现

Given(/^the notification was (successfully|not) sent to the customer "([^"]*)"$/, async function (wasSent, customer, callback) {
    this.message = { sent: {}, received: {} };
    let notificationId = this.notificationProps ? this.notificationProps.guid : this.notificationId;
    this.message.sent.url = this.utility.resolveHost('stubby') + this.config.stubby.root + `/CapturedMessage/NotificationSent-${notificationId}`;
    let request = {
        httpMethod: 'GET',
        url: await LiquidRender.render(this.message.sent.url, this)
    };
    await this.apiContext.sendRequest(request, false, '');
    let counter = 1;
    while (this.apiContext.status === 404 && counter < 12) {
        await this.utility.sleep(1000);
        this.logger.info(`hasn't stored the notification sent to customer. This could mean the notification hasn't been sent yet. Polling ${counter}`);
        let request = {
            httpMethod: 'GET',
            url: await LiquidRender.render(this.message.sent.url, this)
        };
        await this.apiContext.sendRequest(request, false, '');
        counter++;
    }
    let expectedStatusCode = wasSent === 'successfully' ? 200 : 404;
    let msg = `
     some message
    `;

    if (this.apiContext.status !== expectedStatusCode) this.logger.error(msg);
    if (this.apiContext.status !== 404) {
        this.message.sent.stubbyPayload = await this.apiContext.response.json();
        this.apiContext.responseBody = this.message.sent.stubbyPayload.Properties;  
    }   


    let customerResults = new Array();
    

    if (this.customerResults) {
        this.customerResults = [];
    }

    // Capture the result for the current customer
    this.customerResults.push({ customer, result: wasSent});

     customer
    if (wasSent !== 'successfully') {
        throw new Error(`Notification was not successfully sent to ${customer}`);
    }
    callback();
})

我只是想验证通知是否已成功发送给我在场景中指定的每个客户。

我收到以下错误:

TypeError:无法读取未定义的属性(读取“push”)

并且在推送失败的阶段也遇到过

this.customerResults.push({ customer, result: wasSent});
javascript cucumberjs
1个回答
0
投票

你的问题出在你所写的条件上。

    
    if (!this.customerResults) {
        this.customerResults = [];
    }

    // Capture the result for the current customer
    this.customerResults.push({ customer, result: wasSent});

为了确保定义了

this.customerResults
,你已经设置了条件,但它应该是负数,我想确保它不是未定义的?

© www.soinside.com 2019 - 2024. All rights reserved.