如何在AngularJS中为JSON获取表单工厂编写测试用例

时间:2016-04-07 05:09:30

标签: angularjs json

我正在尝试为工厂编写测试cass,这正在撤销JSON响应。

但我收到错误:

错误:[$ injector:unpr] http://errors.angularjs.org/1.4.1/ $ injector / unpr?p0 = serviceProvider%20%3C-%20service     在错误(本机)

这是我的代码:

(function () {
    angular.module('uspDeviceService',[]).factory('getDevice', GetDevice);
    GetDevice.$inject = ['$http'];
    function GetDevice($http) {
            getDeviceList = function() {
                return $http.get("static/test-json/devices/device-list.json");
            }
        return {
            getDeviceList: getDeviceList
        }
    }
}());

测试用例代码:

describe('Get Product test', function() {

    beforeEach(module('uspDeviceService'));
    var service, httpBackend, getDevice ;
    beforeEach(function () {
        angular.mock.inject(function ($injector) {
            //Injecting $http dependencies
            httpBackend = $injector.get('$httpBackend');
            service = $injector.get('service');
            getDevice = $injector.get('getDevice');
        })
    });
    console.log('Injection Dependencies is done');

    describe('get Device List', function () {
        it("should return a list of devices", inject(function () {
            httpBackend.expectGET("static/test-json/devices/device-list.json").respond("Response found!");

            httpBackend.flush();
        }))
    })

});

我是Angular Unit测试的新手,任何人都可以帮助我,我出错的地方......

2 个答案:

答案 0 :(得分:1)

我突然发现了两件事:

  • 您的 $("#btnprint").click(function() { var number = $("#num").val(); var msg = ""; $('input[id^="input_"]').each(function(){ msg += $(this).val() + "<br/>"; }); $("#printdiv").html(msg); }); 声明是定义模块,而不是获取模块。我鼓励你把它分开,以便更清楚你的意图是什么。

    angular.module

    它可能按原样运作,但清晰度很重要。

  • 什么是...... angular.module('uspDeviceService', []); angular.module('uspDeviceService').factory('getDevice', GetDevice); ?它没有在您的代码中的任何位置定义,Angular也找不到它,因此错误消息。您可能希望获得service。另外,根据实际情况命名测试变量,这样就不会让自己感到困惑。

    getDevice

答案 1 :(得分:1)

假设你在myModule中定义了一个angularjs控制器myController。当api调用成功时,控制器执行一些操作,当api返回success = false时,控制器显示flash消息。您的控制器代码类似于

angular.module('myModule')

.controller( 'myController', function ( $scope,flashService, Api ) {


   Api.get_list().$promise.then(function(data){
     if(data.success) {
       $scope.data = data.response
     }
     else{
       flashService.createFlash(data.message, "danger");
     }
  });

});

现在测试success = true和success = false我们

describe('myController', function(){

  var $rootScope, $httpBackend, controller, flashService;

  var apilink = 'http://apilink';

  beforeEach(module('myModule'));

  beforeEach(inject(function(_$httpBackend_,_$rootScope_, _$controller_, _flashService_) {

    $rootScope = _$rootScope_;
    $httpBackend = _$httpBackend_;
    flashService = _flashService_;
    controller =  _$controller_("myController", {$scope: $rootScope});

  }));

 it('init $scope.data when success = true', function(){
   $httpBackend.whenGET(apilink)
   .respond(
    {
     success: true,
     response: {}

    });

    $httpBackend.flush();
    expect($rootScope.data).toBeDefined();
 });

 it('show flash when api request failure', function(){

  spyOn(flashService, 'createFlash');

  $httpBackend.whenGET(apilink)
 .respond(
  {
    success: false
  });

  $httpBackend.flush();
  expect(flashService.createFlash).toHaveBeenCalled();

 });


});

你总是会模仿响应,因为我们正在测试javascript代码行为,而我们并不关心Api。您可以看到数据初始化成功的时间以及成功为false时调用createFlash。

就工厂测试而言,你可以做到

 describe('Get Product test', function() {

    beforeEach(module('uspDeviceService'));
    var service, httpBackend, getDevice ;
    beforeEach(function () {
       inject(function ($injector) {
         httpBackend = $injector.get('$httpBackend');
         service = $injector.get('service');
         getDevice = $injector.get('getDevice');
       });
    });

   describe('get Device List', function () {
     it("should return a list of devices", inject(function () {

       httpBackend.expectGET("static/test-json/devices/device-  list.json").respond("Response found!");
       var result = getDevice.getDeviceList();
       httpBackend.flush();
       expect(result).toEqual('Response found!');

    }));
  });

});