我有問題在嘲笑的資源工廠之上構建茉莉花測試。
的(工作)情況:
控制器:
.controller("remoteCtrl", function(resourceFactory) {
var vm = this;
vm.animals = resourceFactory.getAll();
});
廠
.factory('resourceFactory', function ($resource) {
var demoResource = $resource('/demo-1.0/webresources/animal/:id', {id: '@id'}, {
getAll: {method: 'GET', isArray: true},
getFirst: {method: 'GET', params: {id: 0}, isArray: false},
save: {method: 'POST', params: {id: null}}
});
return demoResource;
});
MockModule爲Backendless發展:
angular.module('playAngular.mock', ['ngMockE2E'])
.run(function ($httpBackend, $log) {
var animals = [
{name: 'Animal1', age: '100', id: 0},
{name: 'Animal2', age: '100', id: 1},
{name: 'Animal3', age: '100', id: 2},
{name: 'Animal4', age: '100', id: 3}
];
$httpBackend.whenGET('/demo-1.0/webresources/animal').respond(function() {
return [200, animals];
});
//....
這是工作的罰款都在一起。但現在問題重重。我想測試我的控制器與茉莉:
describe("Test Suite für RemoteController", function() {
beforeEach(module('ngResource'));
beforeEach(module('ngMockE2E'));
beforeEach(module('playAngular.mock'));
beforeEach(module('playAngular.remote'));
var $controller;
var $scope;
var $rootScope;
beforeEach(inject(function (_$controller_, _$rootScope_) {
$controller = _$controller_;
$rootScope = _$rootScope_;
}));
var controller;
beforeEach(function() {
controller = $controller('remoteCtrl', {$scope: $scope});
});
describe("Basic Remote Test", function() {
it('Test It:', function() {
expect(controller).toBeDefined();
expect(controller.animals).toBe(4);
});
});
});
,測試將失敗:
預計[$承諾:承諾({$$狀態:對象({狀態:0})}),$ resolved:false]爲4.
我明白錯誤,我們在這裏有一個未解決的承諾,但我如何才能讓它在茉莉花測試中等待並測試repond? 這一個:
controller.animals.$promise.then(function(result){
expect(result.length).toBe(4);
});
將無法正常工作,以及(茉莉不等待響應,只是以「所有精」返回
我試試我在網絡上找到的所有提示:觸發消化循環,沖刷httpbackend,但是毫無效果是否有人看到了問題