如何嘲笑的角度单元测试的location.path [英] how to mock a location.path in angular unit tests
问题描述
<一个href=\"http://blog.artlogic.com/2013/05/06/angularjs-best-practices-ive-been-doing-it-wrong-part-2-of-3/\" rel=\"nofollow\">http://blog.artlogic.com/2013/05/06/angularjs-best-practices-ive-been-doing-it-wrong-part-2-of-3/
我测试用location.path裁判路由指令无法找到模板。所以....
I am testing a routing directive with location.path refs to templates that cannot be found. So....
我要嘲笑与配置$ routeProvider一个模拟,如何获得$ routeProvider因果报应/茉莉路由测试?
I want to mock a routing test with a mock configured $routeProvider, how to get the $routeProvider in karma/jasmine?
然后我试着在最佳实践中描述的spyOn模拟的方法,是有,我可以期待$ location.path()语法。TOBE('/路径')?
then I tried the spyOn mock approach described in best-practice, is there a syntax for which I can expect the $location.path().toBe('/path')?
spyOn($location, 'path').andCallFake(new LocationMock().path);
和我在想,如果我能
推荐答案
我创建了一个小提琴这演示嘲讽$位置。
I created a fiddle which demonstrates mocking $location.
app.controller('testcont', function($scope, $location) {
$scope.path = $location.path();
});
...
beforeEach(inject(function($controller, $rootScope, $location){
scope = $rootScope.$new();
spyOn($location, 'path').andReturn('Fake location');
$controller('testcont', {$scope:scope});
}));
...
it('should spy on $location', function($location){
expect(scope.path).toBe('Fake location');
});
但是模板可以用prepopulating角的$ templateCache该指令被加载。
噶使用 NG-html2js- preprocessor 。莫非是帮助你的问题吗?
However templates can be loaded by prepopulating Angular's $templateCache with the directive. Karma uses the ng-html2js-preprocessor. Could that help your problem?
这篇关于如何嘲笑的角度单元测试的location.path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!