2015-11-09 104 views
1

角element.css功能我有一個角向鏈路功能如下:刺探在茉莉花測試

link: function(scope, elem) { 
    elem.css({height: 100%}); 
} 

所以我想測試,如果CSS功能被稱爲在我的測試元件上。這是我曾嘗試並不起作用:

it('should apply correct css', function() { 
    beforeEach(function() { 
     elem = $compile('<my-directive></my-directive>')($scope) 
    }); 

    elem.css = jasmine.createSpy('css').and.returnValue({height: 100%}); 

    expect(elem.css).toHaveBeenCalled(); 
    expect(elem[0].attr('style')).toContain('height: 100%'); 
}); 

我真的不知道要測試這個最好的辦法。我如何完成這項工作?

+1

我通常是描述塊內的各功能,而不是一個「它」塊之前放。 –

+1

@SunilD。這就是我的實際代碼。我只是將它添加到上面的示例代碼中,所以我不必發佈整個代碼。無論如何,謝謝 – Subash

+0

這真是讓人誤解,我認爲你會更好地展現真實的代碼。否則,人們會解僱你的問題。 –

回答

4

爲什麼不只是在沒有間諜的情況下測試指令的效果。見plunker

describe('myDirective', function() { 
    var $scope, $compile, element; 

    beforeEach(module('myApp')); 

    beforeEach(inject(function (_$rootScope_, _$compile_) { 
     $scope = _$rootScope_.$new(); 
     $compile = _$compile_; 
     element = angular.element('<my-directive></my-directive>'); 
    })); 

    it('should set height to 100%', function(){ 
     expect(element[0].style.height).toBe(''); 
     $compile(element)($scope); 
     expect(element[0].style.height).toBe('100%'); 
     expect(element.attr('style')).toContain('height: 100%'); 
    }); 
});