LoginSignup
3
3

More than 5 years have passed since last update.

Protractor で AngularJS で設定した任意のモジュールの値をモック化する

Last updated at Posted at 2015-07-17

Protractor で AngularJS の任意のモジュールの値をモック化する

やりたいこと

Angular で設定した angular.constant の値を、テストの時だけ別の値にモック化したい。

例えば、テスト対象のアプリケーションである処理の UI のローディング時間を 3000 ms と設定している。しかし End-to-End テスト時には、テストにかかる時間を短縮するために 0 ms にしておきたい。

方法

アプリケーションの設定ファイル

app.js

var app = angular.module('my-app');

app.constant('LOADING_TRANTITION', 3000);

テストの設定ファイル

conf.js
exports.config = {
  onPrepare: function() {
    // モック化のための関数
    global.initialize = function() {
      var mockedModule=function(){
        var module = angular.module('oz').config(['$provide', function($provide) {
          $provide.constant('LOADING_TRANTITION','0');
        }]);
      };
      browser.addMockModule('oz', mockedModule);
    };
  }
};
hoge_spec.js

describe('XXX 画面のテスト', function() {

  beforeEach(function() {
    // モック化の関数を実行
    initialize();
  });

  it('....', function() {
    // Test
  };
});

上の例だと、余計な Delay 時間をモック化しておくことで、10 回テストしたとしても、3000 ms * 10 = 30 秒分の節約になった。
これに限らず、テストでモックしたい値がある時は知っておくと楽になるチップスだったので共有しました。

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3