【发布时间】:2013-06-12 23:42:55
【问题描述】:
我创建了一个包含环境信息的Env 服务,我目前正在使用$location.host() 来确定我所处的环境。如何在我的测试中模拟它?
我读过https://groups.google.com/forum/?fromgroups#!topic/angular/F0jFWC4G9hI,但它似乎不起作用,例如:
describe("Env (environment) service", function() {
var Env;
beforeEach(module('App'));
beforeEach(inject(
['Env', function(e) {
Env = e;
}]
));
describe("for staging", function() {
beforeEach(inject(function($location, $rootScope) {
$location.host("http://staging-site.com");
$rootScope.$apply();
}));
it("returns envrionment as staging", function() {
expect(Env.environment).toEqual("staging");
});
it("returns .isStaging() as true", function() {
expect(Env.isStaging()).toBeTruthy();
});
});
});
我也尝试过$browser 变体,但这也不起作用。有什么想法吗?
【问题讨论】:
-
我也按照thecssninja.com/javascript/angular-location-testing 尝试过
$browser.url(),但这也不起作用。 -
我有类似的问题,我需要将 $location 注入我的测试,因为服务需要它。
标签: javascript testing angularjs mocking