为了账号安全,请及时绑定邮箱和手机立即绑定

单元测试AngularJS指令与templateUrl

单元测试AngularJS指令与templateUrl

慕妹3242003 2019-08-15 15:12:48
单元测试AngularJS指令与templateUrl我有一个templateUrl定义的AngularJS指令。我正在尝试用Jasmine进行单元测试。根据以下建议,我的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');             });         });     });});当我在我的Jasmine规范错误中运行它时,我收到以下错误:TypeError: Object #<Object> has no method 'passThrough'我想要的是模板加载原型 - 我不想使用respond。我相信这可能与使用ngMock而不是ngMockE2E有关。如果这是罪魁祸首,我该如何使用后者而不是前者呢?提前致谢!
查看完整描述

3 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

我最终做的是获取模板缓存并将视图放在那里。事实证明,我无法控制不使用ngMock:

beforeEach(inject(function(_$rootScope_, _$compile_, $templateCache) {
    $scope = _$rootScope_;
    $compile = _$compile_;
    $templateCache.put('path/to/template.html', '<div>Here goes the template</div>');}));


查看完整回答
反对 回复 2019-08-15
  • 3 回答
  • 0 关注
  • 510 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信