2015-04-02 59 views
0

我有一個看起來像這樣的單元測試:變化角度模擬值

describe("myDirective Test",function(){ 
    beforeEach(module('app')); 

    beforeEach(function() { 
     module(function($provide) { 
      $provide.constant('Config', {"showmenu":true}); 
     }); 
    }); 

    it('should add showmenu attribute',inject(function($compile,$rootScope){ 
     var element = $compile('<div my-directive></div>')($rootScope); 
     expect(element.attr('showmenu')).not.toBe(undefined); 
    })); 

    it('should NOT add showmenu attribute',inject(function($compile,$rootScope){ 
     var element = $compile('<div my-directive></div>')($rootScope); 
     expect(element.attr('showmenu')).toBe(undefined); 
    })); 
}); 

這是我的指令:

.directive('myDirective', ['$compile','Config', function($compile,Config){ 
    return { 
     scope: true, 
     link: function(scope, element) { 
      if(Config.showmenu){ 
       element.attr('showmenu', "{'height':menuheight,'alwaysVisible':true}"); 
       element.removeAttr('my-directive'); 
       $compile(element)(scope); 
      } 
     } 
    }; 
}]); 

我怎樣才能改變showmenu價值,所以它是falseshould NOT add showmenu attribute測試?

+0

你能在使用'showmenu'的地方共享指令模板? – Chandermani 2015-04-02 08:48:06

+0

@Chandermani我用我的指令示例更新了我的問題。那麼,怎麼做呢?謝謝。 – user1995781 2015-04-02 09:06:14

回答

1

嘗試注入配置您的測試,並且在那裏修改:

it('should NOT add showmenu attribute',inject(function($compile,$rootScope, Config){ 
    Config.showmenu = false; 
    var element = $compile('<div my-directive></div>')($rootScope); 
    expect(element.attr('showmenu')).toBe(undefined); 
})); 

不知道你真正的代碼是這樣的,但你在這裏有一個額外的空間:

.directive('myDirective '