这是我的代码,我收到以下错误,无法弄明白。
describe('Myctrl', function() {
var $httpBackend, scope, createController, authRequestHandler;
// Set up the module
beforeEach(module('myApp'));
alert("Hello there!");
beforeEach(inject(function ($injector) {
// Set up the mock http service responses
alert("Hello there!");
$httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
alert("Hello there!");
authRequestHandler = $httpBackend.when('GET', 'http://www.w3schools.com/angular/customers.php')
.respond(true);
// Get hold of a scope (i.e. the root scope)
$rootScope = $injector.get('$rootScope');
// The $controller service is used to create instances of controllers
var $controller = $injector.get('$controller');
createController = function() {
return $controller('Myctrl', {'$scope' : scope});
};
})
);
/*
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});*/
it('should fetch authentication token', function() {
//create expectation
$httpBackend.expectGET('http://www.w3schools.com/angular/customers.php');
var controller = createController()
$httpBackend.flush();
expect(scope.names).toBeTruthy(true);
});
});
我尝试了一些不同的东西,但我仍然无法弄清楚出了什么问题。好像没有错。这种情况的解决方案是什么?
答案 0 :(得分:0)
我重构了你的代码。但是我不确定问题是否存在。我认为您也可能没有包含ngMock(https://docs.angularjs.org/api/ngMock),这就是您收到此类错误的原因
describe('Myctrl', function () {
var $httpBackend, $scope, createController;
// Set up the module
beforeEach(module('myApp', ['ngMock']));
beforeEach(inject(['$httpBackend', '$rootScope', '$controller', function (_$httpBackend_, $rootScope, $controller) {
// Set up the mock http service responses
$httpBackend = _$httpBackend_;
// backend definition common for all tests
$httpBackend.whenGET('http://www.w3schools.com/angular/customers.php').respond(function () {
return [200, true];
});
// Get hold of a scope (i.e. the root scope)
$scope = $rootScope.$new();
createController = function () {
return $controller('Myctrl', { '$scope': $scope });
};
}]));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should fetch authentication token', function () {
//create expectation
$httpBackend.expectGET('http://www.w3schools.com/angular/customers.php');
var controller = createController();
$httpBackend.flush();
expect($scope.names).toBeTruthy();
});
});
答案 1 :(得分:0)
首先,我们必须声明在茉莉花中使用$ htttp服务的假后端。
describe("Test Add Category JSON",function(){
'use strict';
var scope, ctrl , $httpBackend;
beforeEach(inject(function($controller,$rootScope){
scope = $rootScope.$new();
ctrl = $controller('View1Ctrl',{$scope:scope});
}));
beforeEach(inject(function(_$httpBackend_){
$httpBackend = _$httpBackend_;
}));
it('Test Load JSON Name',function(){
$httpBackend.whenGET('../json/login/getCategory.json').respond({
"categoryID": 1,
"coreIndustry": "Prime Video",
"subIndustry": "Prime Video",
"classification12": "Easy"
})
scope.loadJson();
$httpBackend.flush();
expect(scope.data.categoryID).toEqual(1);
});
});