2016-04-07 5 views
1

JSONレスポンスを返却するファクトリ用のテストキャスを作成しようとしています。AngularJSでフォームファクトリーを取得するためのテストケースの記述方法

エラー:[$インジェクター:UNPR]?http://errors.angularjs.org/1.4.1/ $インジェクター/ UNPR P0 =サービス・プロバイダ%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(); 
     })) 
    }) 

}); 

誰もが私が間違っているつもりどこ、私を助けてくださいすることができ、私は、角度ユニットテストに新しいです..私に飛び出す

答えて

1

2つのこと:

  • あなたangular.module宣言のモジュールであり、のモジュールではありません。モジュールを取得しています。私はあなたの意図が何であるかがはっきりしているように、それを分割することをお勧めします。

    angular.module('uspDeviceService', []); 
    angular.module('uspDeviceService').factory('getDevice', GetDevice); 
    

    おそらくそのまま動作しますが、明快さが重要です。

  • とは... serviceとは?これはコード内のどこにも定義されておらず、Angularもそれを見つけることができないため、エラーメッセージが表示されます。あなたはgetDeviceを代わりに探しているかもしれません。また、テスト変数の名前を実際のものに合わせて指定してください。混乱しないようにしてください。あなたがMyModuleという中で定義されたangularjsコントローラmyControllerを持っていると仮定すると

    // defined above 
    var getDevice; 
    
    // while injecting 
    getDevice = $injector.get('getDevice'); 
    
+0

[OK]を行うことができます懸念している限り? –

+0

@AnitaMehta:これは単体テストなので、JSONが常に正確に戻ってくると仮定したいと思うでしょう。実際のエンドポイントをテストする場合は、2つ目の注意が必要です。サードパーティ製のため、脆くなります。これらのテストは、エンドツーエンドのテストに最適です。 – Makoto

+0

データがフォームファクトリに戻ってきているかどうかをチェックしたいのですが、それはすべて –

1

。コントローラは、api呼び出しが成功すると何らかのアクションを行い、apiがsuccess = falseを返したときにフラッシュメッセージを表示します。お使いのコントローラのコードは、あなたはいつもここに私たちはjavascriptのコードをテストしているので、応答を模擬しようとしている今

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"); 
    } 
    }); 

}); 

のようなものの両方の成功をテストする=真の成功= 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(); 

}); 


}); 

だろう私たちはApiに関心がありません。成功したときにデータが初期化され、成功するとfalseが返されます。createFlashが呼び出されます。

JSONが来ているかどうかの工場のためのテストは、あなたが、私はそれを得た後、どのようにテストするために

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!'); 

    })); 
    }); 

}); 
+0

ですが、ここではテスト目的のためだけに工場の個人をテストしようとしています。工場で書いていることを教えてください。 JSONレスポンス内のオブジェクトを比較してテストします。 –

+0

工場に追加されたテスト – akniazi

+0

これでエラーが発生しました:Expected Object({$$状態:オブジェクト({ステータス:1、値:オブジェクト({データ: 'レスポンスが見つかりました!'、ステータス:200、ヘッダー:機能、設定:Object({accept: 'application/json、text/plain、*/*'}):Object({metho amSerializer:Function、url: 'static/test-json/devices/device-list.json'、ヘッダー:オブジェクト})、 e found! '。 –