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

时间:2015-03-18 22:45:12

标签: javascript testing jasmine protractor jasmine-matchers

这是失败的测试:

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();
    });
});

其中MyPage Page Object

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;

其中errors应该是在页面上找到的可见错误文本数组

以下是我们遇到的错误:

Failures:

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

仅供参考,toBeEmptyArray()匹配器来自jasmine-matchers第三方。


我试图以这种方式打印scope.page.errors的值:

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

它打印为[]Array.isArray(errors)会返回true

从我看来,scope.page.errors是一个空数组,但期望失败。我失踪了什么?

2 个答案:

答案 0 :(得分:3)

答案是protractor src中的四行。

ElementArrayFinder extends Promise,而jasmine-matchers检查first checks错误是一个真正的数组Array.isArray is done,它将返回false;

这也与expect(scope.page.errors.length).toBe(0)未定义一致,因为promises没有长度。

只需对您的承诺运行errors.then,并测试参数是否为[] 您还表明可以在运行scope.page.errors.then

时完成

答案 1 :(得分:0)

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();
    });
});