4

順調に進んでいる単体テストがたくさんあり、プロジェクトに分度器 E2E テストを追加し始めました。ページ上のインタラクティブ要素のテストは問題なく行っていますが、ブラウザから送信される特定のデータのテストに問題があります。

たとえば、特定のボタンをクリックするPOSTと、特定のエンドポイントが生成されるかどうかを確認したいと考えています。

以下を使用して分度器をセットアップしました。

/*globals global*/
module.exports = function() {
    'use strict';
    var chai = require('chai')
    ,   promised = require('chai-as-promised');

    global.expect = chai.expect;

    chai.use(promised);
}();

分度器を使用してやり取りする方法を理解しています。

it('send data to the "log" endpoint when clicked', function() {
    var repeater = element.all(by.repeater('finding in data.items'));

    repeater.get(0).click().then(function() {
        // $http expectation
    });
});

ただし、イベント$httpBackendの結果として送信されるデータをキャプチャできるように分度器を設定する方法がわかりません。.click()追加のモジュールが必要ですか?

Karma/Mocha では、単純に次のようにします。

beforeEach(module('exampleApp'));

describe('logging service', function() {
    var $httpPostSpy, LoggingService;

    beforeEach(inject(function(_logging_, $http, $httpBackend) {
        $httpPostSpy = sinon.spy($http, 'post');
        LoggingService = _logging_;
        backend = $httpBackend;
        backend.when('POST', '/api/log').respond(200);
    }));

    it('should send data to $http.post', function() [
        LoggingService.sendLog({ message: 'logged!'});
        backend.flush();
        expect($httpPostSpy.args[0][1]).to.have.property('message');
    });
});

しかし、分度器$httpBackendでモジュールへの参照を取得する方法がわかりません。inject

4

2 に答える 2

0

$httpBackend は、サーバーへの偽の呼び出しをモックするためのものです。e2e テストでは、通常、実際にサーバーを呼び出します。分度器のほとんどの要素ロケーターがpromises を返すことに注意することが重要です。

つまり、このコードを使用すると、テストはサーバーからの応答が返されるまで待機し、テキストが p タグであることをアサートして、サーバーからの正しいデータであることがわかります。

my-file.spec.js

    'use strict';

describe('The main view', function () {
  var page;

  beforeEach(function () {
    browser.get('/index.html');
    page = require('./main.po');
  });

  it('should have resultText defined', function () {
      expect(page.resultText).toBeDefined()
  })


  it('should display some text', function() {
    expect(page.resultText.getText()
      .then())
      .toEqual("data-that-should-be-returned");
  });


});

my-file.po.js

'use strict';

var MainPage = function() {
  this.resultText = element(by.css('.result-text'));


};

module.exports = new MainPage();
于 2016-05-22T06:11:33.540 に答える