简体   繁体   English

期望失败:“预期[]为空数组。”

[英]Failed expectation: “Expected [ ] to be empty array.”

Here is the failing test: 这是失败的测试:

describe("Checking errors", function () {
    var scope = {};

    beforeEach(function () {
        browser.get("/#endpoint");
        browser.waitForAngular();

        scope.page = new MyPage();
    });

    it("should not show any errors", function () {
        expect(scope.page.errors).toBeEmptyArray();
    });
});

where MyPage is a Page Object : 其中MyPage页面对象

var MyPage = function () {
    this.errors = element.all(by.css("div.error-block b.error"))
        .filter(function (elm) {
            return elm.isDisplayed().then(function (value) {
                return value;
            });
        })
        .map(function (elm) {
            return elm.getText();
        });
};

module.exports = MyPage;

where errors supposed to be an array of visible error texts found on a page. errors应该是在页面上找到的可见错误文本数组

Here is the error we are getting: 这是我们得到的错误:

Failures:

  1) Checking errors should not show any errors
   Message:
     Expected [  ] to be empty array.
   Stacktrace:
     Error: Failed expectation

FYI, toBeEmptyArray() matcher is coming from jasmine-matchers third-party. 仅供参考, toBeEmptyArray() matcher来自jasmine-matchers toBeEmptyArray()第三方。


I've tried to print out the value of scope.page.errors this way: 我试图用这种方式打印出scope.page.errors的值:

scope.page.errors.then(function (errors) {
    console.log(errors);
});

And it is printed out as [] . 它以[]打印出来。 Array.isArray(errors) returns true . Array.isArray(errors)返回true

From what I see, scope.page.errors is an empty array, but the expectation fails. 从我看到, scope.page.errors是一个空数组,但期望失败。 What I a missing? 我错过了什么?

the answer is four lines down in the protractor src . 答案是量角器src中的四行。

ElementArrayFinder extends Promise , while jasmine-matchers checks first checks that errors is an actual array exactly how Array.isArray is done , which will return false; ElementArrayFinder extends Promise ,而jasmine-matchers检查首先检查错误是否是Array.isArray完成的实际数组,这将返回false;

This is also consistent with expect(scope.page.errors.length).toBe(0) being undefined because promises do not have lengths. 这也与expect(scope.page.errors.length).toBe(0)未定义一致,因为promises没有长度。

Just run errors.then on your promise, and test that the argument is [] You've also shown that can be done when you ran scope.page.errors.then 只是运行errors.then你的承诺,并测试参数是[]你还表明,当你运行scope.page.errors.then时可以完成

Inside test script your code line "scope.page = new MyPage();" is creating new empty MyPage object.Code which you have written in application script is creating page object locally and its not bounded with any angular scope.When there is requirement of testing such objects you need to replicate code for page object creation in beforeEach(); block of test script.And test it.
 describe("Checking errors", function () {
    var scope = {};
     var MyPage ;
    beforeEach(function () {
        browser.get("/#endpoint");
        browser.waitForAngular();

       MyPage = function () {
    this.errors = element.all(by.css("div.error-block b.error"))
        .filter(function (elm) {
            return elm.isDisplayed().then(function (value) {
                return value;
            });
        })
        .map(function (elm) {
            return elm.getText();
        });
};
    });

    it("should not show any errors", function () {
        expect(MyPage.errors).toBeEmptyArray();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在数组中使用 0 时过滤器的行为不符合预期。 Javascript - Filter not behaving as expected with 0 in an array. Javascript IBM SBT:预期失败错误 - IBM SBT :Expectation Failed error 将数组中的项目移动到另一个数组。 我得到的错误是“预期标识符” - Moving items in array to another array. The error I get is 'Expected identifier' guild.channels.cache.array() 返回一个空数组。 与 client.channels.cache.array() 相同 - guild.channels.cache.array() returns an empty array. same with client.channels.cache.array() 使用数组将预期事件参数解构为仅目标属性。 查找方法 - de-structure expected event parameter to just the target property , using array. find method 返回String.match.length的数组。 预期为整数值。 - String.match.length returning array. Expected an integer value. 数组中的散布操作出错。 TS1005:“,”应为。 打字稿 - Error with spread operation in array. TS1005: ',' expected. TypeScript Javascript数组。 高级 - Javascript array. Advanced socket.io连接返回一个空数组。 如何在React中接收数据而不重新加载页面? - The socket.io connection returns an empty array. Ways to receive data in React without reloading the page? 如果元素在数组中有一个空值。 然后告诉所有下一项为假 - If the element has an empty value in the array. Then tell all the next items false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM