ウェブサイト検索

javascript - 失敗した期待: 「[ ] が空の配列であることを期待しました。」


こちらが失敗しているテストです:

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 は ページオブジェクト です:

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 は空の配列ですが、期待が失敗します。何を見落としているのでしょうか?

回答 #1

答えは分度器のソースの4行下にあります。

`ElementArrayFinder extends Promise` 一方で、jasmine-matchers は最初に errors が実際の配列であることをチェックします。これは Array.isArray が行う方法とまったく同じで、false を返します;

これは、プロミスには長さがないため、expect(scope.page.errors.length).toBe(0) が未定義であることとも一致します。

あなたのプロミスで errors.then を実行し、引数が [] であることをテストしてください。また、scope.page.errors.then を実行したときにそれができることも示しています。

回答 #2

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