情况:
我需要测试一个http调用并确保它被正确调用。
在应用程序中一切正常:$ scope.language_list从api正确检索数据
但是在测试中,$ scope.language_list是未定义的。 所以我可能没有正确地进行测试。
代码:
功能:
$scope.get_language_list = function()
{
$http.get('http://my_app.org/languageList')
.success(function(data, status, headers, config)
{
$scope.language_list = data;
$scope.valid = true;
})
.error(function(data, status, headers, config)
{
$scope.valid = false;
});
}
测试:
describe('Http calls test', function() {
beforeEach(module('my_app.controllers'));
beforeEach(inject(function(_$controller_, _$httpBackend_)
{
$scope = {};
var controller = $controller('MainCtrl', { $scope: $scope });
$httpBackend = _$httpBackend_;
$httpBackend.whenGET('http://my_app.org/languageList').respond(200);
}));
it('should load default language list', function()
{
$httpBackend.flush();
console.log($scope.language_list);
expect($scope.valid).toBe(true);
expect($scope.language_list).not.toEqual(undefined);
});
});
结果:
$ scope.valid 测试正常。如果我故意在函数中更改其值以查看测试失败,则它会失败,否则会正确传递为true。
但是 $ scope.language_list 无法正常工作,并且作为未定义传递。实际上,错误消息是:Expected undefined not to equal undefined.
问题:
如何正确测试http电话? 为什么$ scope.language_list未定义?我在测试中做错了什么?
非常感谢!
答案 0 :(得分:3)
您需要提供模拟响应对象。目前"数据"成功函数中的参数未定义。要定义响应:
beforeEach(inject(function(_$controller_, _$httpBackend_)
{
$scope = {};
var controller = $controller('MainCtrl', { $scope: $scope });
$httpBackend = _$httpBackend_;
//Tell the $httpBackend to respond with our mockLangularList array. Or whatever the api actually returns.Array used for example.
var mockLanguageList = [{key: ''},{key: ''},{key: ''}];
$httpBackend.whenGET('http://my_app.org/languageList').respond(200, mockLanguageList);
}));
另外,你们每个人都调用$ scope.get_language_list()吗?
it('should load default language list', function()
{
$scope.get_language_list(); // <-- Make call
$httpBackend.flush();
console.log($scope.language_list);
expect($scope.valid).toBe(true);
expect($scope.language_list).not.toEqual(undefined);
});