AngularJS更改传递给配置以进行单元测试的常量值 [英] AngularJS Change Constant Value that gets passed to Config for Unit Test

查看:14
本文介绍了AngularJS更改传递给配置以进行单元测试的常量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以更改发送到模块配置函数以进行单元测试的常量值?

我有以下(小提琴这里):

//--- 代码 --------------------------var module = angular.module("myApp", []);module.constant("myConstant", "foo");module.provider("awesomeStuff", function () {变量值;this.setValue = function (val) {值 = 值;};this.$get = function () {返回 {我的价值:价值};};});module.config(function (myConstant, awesomeStuffProvider) {//想模拟myConstantawesomeStuffProvider.setValue(myConstant);});//- - 眼镜  -  -  -  -  -  -  -  -  -  -  -  - -描述(富",函数(){beforeEach(angular.mock.module("myApp", function ($provide) {//尝试覆盖传递给配置的 myConstant 值$provide.constant("myConstant", "bar");}));it("值为 bar", inject(function(awesomeStuff, $injector) {期望($injector.get("myConstant")).toBe("bar");期望(awesomeStuff.myValue).toBe(酒吧");}));});

我知道这是一个微不足道的例子,但我想知道是否可以将不同的常量注入到配置中...我知道可以获取对提供程序的引用并调用 setValue 函数单元测试(即通过this SO post配置提供程序),但这不是我要找的.

感谢您的帮助.

解决方案

考虑以下几点:

beforeEach(angular.mock.module("myApp"));

这将加载模块并执行注册的配置功能.<​​/p>

在你的情况下,你有:

beforeEach(angular.mock.module("myApp", function ($provide) {//尝试覆盖传递给配置的 myConstant 值$provide.constant("myConstant", "bar");}));

现在发生的情况与您遇到以下情况基本相同:

var module = angular.module("myApp", []);...省略常量和提供者...module.config(function(myConstant, awesomeStuffProvider) {awesomeStuffProvider.setValue(myConstant);});模块配置(功能($提供){$provide.constant("myConstant", "bar");});

由于注册的配置函数将按照注册的顺序执行,因此不会有预期的结果.

如果您需要在将常量用于任何配置函数之前对其进行模拟,我建议将其放在单独的模块中.

根据您的用例,您现在可以:

  1. 创建此模块的两个版本 - 一个用于生产,一个用于测试
  2. 仅创建真实版本并在测试中模拟常量

第二种情况看起来像这样:

应用:

angular.module("configuration", []).constant("myConstant", "foo");var module = angular.module("myApp", ["configuration"]);module.provider("awesomeStuff", function () {变量值;this.setValue = function (val) {值 = 值;};this.$get = function () {返回 {我的价值:价值};};});module.config(function (myConstant, awesomeStuffProvider) {awesomeStuffProvider.setValue(myConstant);});

测试:

describe("foo", function () {之前(函数(){angular.mock.module("配置", function ($provide) {$provide.constant("myConstant", "bar");});angular.mock.module("myApp", function () {//别的});});it("值为 bar", inject(function (awesomeStuff, $injector) {期望($injector.get("myConstant")).toBe("bar");期望(awesomeStuff.myValue).toBe(酒吧");}));});

JSFiddle: http://jsfiddle.net/f0nmbv3c/

Is there anyway to change the constant value that gets sent to the config function of the module for a Unit Test?

I have the following (fiddle here):

//--- CODE --------------------------
var module = angular.module("myApp", []);
module.constant("myConstant", "foo");
module.provider("awesomeStuff", function () {
    var value;

    this.setValue = function (val) {
        value = val;
    };

    this.$get = function () {
        return {
            myValue: value
        };
    };
});
module.config(function (myConstant, awesomeStuffProvider) {
    //want to mock myConstant
    awesomeStuffProvider.setValue(myConstant);
});



//--- SPECS -------------------------
describe("foo", function() {
    beforeEach(angular.mock.module("myApp", function ($provide) {
        //Attempt to override the myConstant value that gets passed to config
        $provide.constant("myConstant", "bar");
    }));

  it("has a value of bar", inject(function(awesomeStuff, $injector) {
    expect($injector.get("myConstant")).toBe("bar");
    expect(awesomeStuff.myValue).toBe("bar");
  }));
});

I know this is a trivial example, but I want to know if its possible to get a different constant to be injected into the config... I know it is possible to get a reference to the provider and call setValue function from unit test (i.e. configuring provider via this SO post), but this is not what I'm looking for.

Thanks for any help.

解决方案

Consider the following:

beforeEach(angular.mock.module("myApp"));

This will load the module and execute the registered config functions.

In your case you have:

beforeEach(angular.mock.module("myApp", function ($provide) {
    //Attempt to override the myConstant value that gets passed to config
    $provide.constant("myConstant", "bar");
}));

What happens now is basically the same as if you had the following:

var module = angular.module("myApp", []);

... Constant and Provider omitted ...

module.config(function(myConstant, awesomeStuffProvider) {

  awesomeStuffProvider.setValue(myConstant);
});

module.config(function($provide) {

  $provide.constant("myConstant", "bar");
});

As the registered config functions will be executed in order of registration this will not have the desired result.

If you need to mock a constant before it's used in any config function I would recommend to put it in a separate module.

Depending on your use case, you can now either:

  1. Create two versions of this module - one for production and one for the tests
  2. Create only the real version and mock the constant in the test

The second case would look something like this:

App:

angular.module("configuration", []).constant("myConstant", "foo");

var module = angular.module("myApp", ["configuration"]);

module.provider("awesomeStuff", function () {
    var value;

    this.setValue = function (val) {
        value = val;
    };

    this.$get = function () {
        return {
            myValue: value
        };
    };
});

module.config(function (myConstant, awesomeStuffProvider) {
    awesomeStuffProvider.setValue(myConstant);
});

Test:

describe("foo", function () {

    beforeEach(function () {

        angular.mock.module("configuration", function ($provide) {
            $provide.constant("myConstant", "bar");
        });

        angular.mock.module("myApp", function () {
            // Something else
        });
    });

    it("has a value of bar", inject(function (awesomeStuff, $injector) {
        expect($injector.get("myConstant")).toBe("bar");
        expect(awesomeStuff.myValue).toBe("bar");
    }));
});

JSFiddle: http://jsfiddle.net/f0nmbv3c/

这篇关于AngularJS更改传递给配置以进行单元测试的常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆