尝试在我的茉莉花脚本中调用我的ngapp角色服务:
it('should create client', function () {
browser.executeAsyncScript(function(callback) {
var api = angular.injector(['$http','myservices']).get('custService');
api.saveClient({name:'Ted'},function(data){
console.log(data);
callback(data);
});
});
});
我的问题是如何传递http依赖项,因为现在我收到此错误:
UnknownError: javascript error: [$injector:modulerr] Failed to instantiate module $http due to:
Error: [$injector:nomod] Module '$http' is not available! You either misspelled the module name or forgot to load it.
答案 0 :(得分:0)
您应该使用模拟数据而不是创建真实对象。
做一个真正的http请求并不是一个好主意。 @merlin在评论中说,在回复中使用模拟。尝试使用$ httpBackend来模拟你的回复。
文件引用:
$ http服务使用$ httpBackend服务将请求发送到真实服务器
你可以在示例中注入你的模拟:
beforeEach(
inject(function($injector) {
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
authRequestHandler = $httpBackend.when('GET', '/auth.py')
.respond({userId: 'userX'}, {'A-Token': 'xxx'});
// ...
}
如果你还需要http服务,你应该像下面那样注入它
var myHttpService;
beforeEach( inject(function($http) {
myHttpService = $http;
}));
之后,您可以预期会发生http GET。你应该调用$ httpBackend.flush,因为在测试场景中它不会运行async而$ http在后台使用promises。请参阅文档中的示例。