在我的测试中,我收到此错误:
Error: No pending request to flush !
at Function.$httpBackend.flush (/home/arpho/Projects/neo4Scrum/app/bower_components/angular-mocks/angular-mocks.js:1482:34)
at null.<anonymous> (/home/arpho/Projects/neo4Scrum/test/spec/controllers/customer.js:95:18)
这是我的代码:
'use strict';
describe('Controller: CustomerCtrl', function () {
// load the controller's module
beforeEach(module('neo4ScrumApp'));
var MainCtrl,
scope,
$httpBackend;
// Initialize the controller and a mock scope
beforeEach(inject(function (_$httpBackend_, $controller, $rootScope) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('/api/customer/:25618')
.respond({columns:['r','c','o'],
data:[
{data:{ name: 'Giuseppe', surname: 'D\'Amico', note: 'it\'s me' },id: '25618'},
{data:{use:'residenza'},type: 'LIVES_IN'},// relazione indirizzo
{data: // item
{ }
},
{data:{ name: 'Giuseppe', surname: 'D\'Amico', note: 'it\'s me' },id: '25618'},
{data:{use:'domicilio'},type: 'LIVES_IN'},
{data:
{ c
},
},
{
data: { company: 'H3G', use: 'principale' },
type: 'ANSWERS_TO'
},
{
data: {
number: lllll,
note: 'intestato a Marco'
},
},
{data:{ name: 'Giuseppe', surname: 'D\'Amico', note: 'it\'s me' },
id: '25618'
},
{
data: { use: 'extra' },
type: 'RECEIVES'
},
{
data: { mail: 'mail',
use: 'extra'
}
},
{data:{ name: 'Giuseppe', surname: 'D\'Amico', note: 'it\'s me' },
id: '25618'
},
{
data: { use: 'principal' },
type: 'RECEIVES'
},
{
data: {
mail: 'mail',
use: 'principal'
}
},
]});
scope = $rootScope.$new();
MainCtrl = $controller('CustomerCtrl', {
$scope: scope
});
}));
it('should attach a list of customers with one only item', function () {
//expect(scope.awesomeThings).toBeUndefined();
console.log('end test');
$httpBackend.flush();
console.log(scope.customers);
expect(scope.customers.length).toBe(1);
});
});
我看到其他人有同样的问题,但我无法解决 我的MainCtrl:
'use strict';
angular.module('neo4ScrumApp').controller('MainCtrl', function ($scope, $http) {
http.get('/api/awesomeThings').success(function(aw){$scope.awesomeThings = aw;})
});
my CustomersCtrl:
'use strict';
angular.module('neo4ScrumApp').controller('CustomerCtrl',['$scope','$http','$routeParams', function($scope,$http,$routeParams) {
var customerId = $routeParams.customerId; // rimuovo :
if (typeof customerId != 'undefined') {
console.log("cerco customer "+customerId);
$http.get('/api/customer/:'+customerId).success(function(customer) {
$scope.customer = customer.data[0].data;
$scope.customer.id = customer.id;
$scope.action = 'update';
})
}
else
$scope.action = 'save';
}])