AngularJs单元测试:“不满意的请求:删除”

时间:2015-04-06 12:20:14

标签: angularjs unit-testing jasmine angular-mock

获取"不满意的请求:删除"我的控制器测试。下面是我的服务,控制器和测试的代码。测试是使用jasmine-karma和angular-mock编写的。提前谢谢。

服务:

function deleteAsset(assetId) {
        return $http.delete(baseUrl + '/delete/' + assetId).then(assetDeleted);

        function assetDeleted(response) {
            return response.data;
        }
    }  

控制器:

(function () {
'use strict';

angular.module('tbApp').controller('assetEditCtrl', assetEditCtrl);


assetEditCtrl.inject = ['$routeParams', 'Campaign', 'User', '$location', 'Asset', '$window', '$scope', '$compile', 'ImageLibrary', 'Lists'];

function assetEditCtrl($routeParams, Asset, $window, $scope, $compile) {

     function deleteAsset(assetId,assetType,assetName, el) {
    //confirmation popup 
    CB.setProceedBoxCode("Are you sure you want to delete "+assetType+" "+assetName+"?", el, {OK: "Yes", CANCEL: "No", width: "400", title: "", callback: function (r, ele) {

            if (r == true) {

                Asset.deleteAsset(assetId).then(assetDeleted);
            }

        }});


    function assetDeleted(data) {

        backTodashbiard();
    }
}  } });

测试:

describe('assetEditCtrl', function() {
var scope, httpBackend, http, controller,  vm, createController, assetId, el, assetType, assetName, compile;

var baseUrl = './webresources/assets';

beforeEach(module("tbApp"));
beforeEach(inject(function($rootScope, $httpBackend, $controller, $http, $compile) {
    httpBackend = $httpBackend;
    compile = $compile
    scope = $rootScope.$new();
    controller = $controller;
    assetId= "EML1000006003";
    campaignId="CMP1000004385";

    createController = function() {
    var controller = $controller('assetEditCtrl', {
        '$scope': scope
    });


    return controller;
};
}));

afterEach(function() {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
})

it('should be able to delete an asset', function(){

    vm=createController();

    assetType="email";
    assetName="Untitled";

    httpBackend.expectDELETE(baseUrl+'/delete/'+assetId).respond({});
    vm.deleteAsset(assetId,assetType,assetName, el);
    httpBackend.flush();

});});

输出:

    Error: Unsatisfied requests: DELETE ./webresources/assets/delete/EML1000006003 in D:/Tb_test/src/main/webapp/js/libraries/uncompressed/angular-mocks.js
 createHttpBackendMock/$httpBackend.verifyNoOutstandingExpectation@D:/Tb_test/src/main/webapp/js/libraries/uncompressed/angular-mocks.js:1488:7
 createHttpBackendMock/$httpBackend.flush@D:/Tb_test/src/main/webapp/js/libraries/uncompressed/angular-mocks.js:1467:5
 @D:/Tb_test/src/test/js/assetEditCtrlSpec.js:164:3
 env.executeFiltered@D:/Tb_test/node_modules/karma-jasmine/lib/boot.js:117:7
 createStartFn/<@D:/Tb_test/node_modules/karma-jasmine/lib/adapter.js:171:5
 [2]</Karma/this.loaded@http://localhost:9876/karma.js:185:7

0 个答案:

没有答案