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

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

问题描述

有没有改变发送到单元测试的模块的配置功能的常量值?



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

  // --- 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){
//想要模拟myConstant
awesomeStuffProvider.setValue(myConstant);
});



// --- SPECS -------------------------
describe(foo,function(){
beforeEach(angular.mock.module(myApp,function($ provide){
//尝试覆盖传递给config的myConstant值
$ provide.constant(myConstant,bar);
}));

it(有一个值bar,inject(function(awesomeStuff, $ injector){
expect($ injector.get(myConstant))。toBe(bar);
expect(awesomeStuff.myValue).toBe(bar);
}));
});

我知道这是一个简单的例子,但我想知道是否有可能得到一个不同的常数注入到配置中......我知道有可能获得对提供者的引用并从单元测试中调用setValue函数(即通过这个SO帖子),但这不是我想要的。



感谢您的帮助。

解决方案

请考虑以下事项:

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

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

在你的情况下你有:

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

现在发生的情况与您拥有以下内容基本相同:

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

...常数和提供者省略...

module.config(函数(myConstant,awesomeStuffProvider){

awesomeStuffProvider.setValue(myConstant) );
});

module.config(函数($ provide){

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

由于注册的配置功能将按注册顺序执行,因此无法获得所需的结果。



如果你需要在任何配置函数中使用之前模拟一个常量,我建议把它放在一个单独的模块中。



根据您的使用情况,您现在可以:


  1. 创建此模块的两个版本 - 一个用于生产,一个用于生产测试

  2. 仅创建真实版本并在测试中模拟常量

第二个case看起来像这样:



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

测试:

  describe(foo,function(){

beforeEach(function(){

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

angular.mock.module(myApp,function(){
//其他的东西
});
});

it(有一个值bar,inject(function(awesomeStuff,$ injector){
expect($ injector.get(myConstant))。toBe(bar);
expect(awesomeStuff.myValue).toBe(bar);
}));
});

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更改常量值,传递给Config进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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