有一个指令。
指令定义了
templateUrl
。指令需要进行单元测试。
目前使用Jasmine进行单元测试。 这个建议使用以下代码:
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'
需要按原样加载templateUrl
无法使用respond
.passThrough();
,但是从文档中来看,你是否尝试过像这样的东西:$httpBackend.expectGET('path/to/template.html'); // do action here $httpBackend.flush();
我认为这更适合你的用法 - 你不想捕获请求,即whenGet()
,而是要检查它是否被发送,然后实际发送它? - Alex OsbornexpectGet
只是检查是否尝试了一个请求。 - Alex Osborn$httpBackend
模拟器实际上使用指令中提供的templateUrl
下的URL并获取它。我以为passThrough
会做到这一点。你知道其他的方法吗? - Jesus is Lord