單元測(cè)試AngularJS指令與templateUrl我有一個(gè)templateUrl定義的AngularJS指令。我正在嘗試用Jasmine進(jìn)行單元測(cè)試。根據(jù)以下建議,我的Jasmine JavaScript如下所示:describe('module: my.module', function () {
beforeEach(module('my.module'));
describe('my-directive directive', function () {
var scope, $compile;
beforeEach(inject(function (_$rootScope_, _$compile_, $injector) {
scope = _$rootScope_;
$compile = _$compile_;
$httpBackend = $injector.get('$httpBackend');
$httpBackend.whenGET('path/to/template.html').passThrough();
}));
describe('test', function () {
var element;
beforeEach(function () {
element = $compile(
'<my-directive></my-directive>')(scope);
angular.element(document.body).append(element);
});
afterEach(function () {
element.remove();
});
it('test', function () {
expect(element.html()).toBe('asdf');
});
});
});});當(dāng)我在我的Jasmine規(guī)范錯(cuò)誤中運(yùn)行它時(shí),我收到以下錯(cuò)誤:TypeError: Object #<Object> has no method 'passThrough'我想要的是模板加載原型 - 我不想使用respond。我相信這可能與使用ngMock而不是ngMockE2E有關(guān)。如果這是罪魁禍?zhǔn)祝以撊绾问褂煤笳叨皇乔罢吣??提前致謝!
3 回答

慕慕森
TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
我最終做的是獲取模板緩存并將視圖放在那里。事實(shí)證明,我無(wú)法控制不使用ngMock:
beforeEach(inject(function(_$rootScope_, _$compile_, $templateCache) { $scope = _$rootScope_; $compile = _$compile_; $templateCache.put('path/to/template.html', '<div>Here goes the template</div>');}));
- 3 回答
- 0 關(guān)注
- 604 瀏覽
添加回答
舉報(bào)
0/150
提交
取消