2016-06-20 5 views
0

このifステートメントにアクセスしようとしましたが、私のカバレッジは私がそうではないと言っています。何かご意見は?前もって感謝します!ifステートメントのテスト - AngularJS/Jasmine

'use strict'; 

describe('Service: configService', function() { 

    // load the controller's module 
    beforeEach(module('Service')); 

    var configService, scope, httpBackend, results, tstConfigObj; 
    var tstConfig = { 
    "confLocation": "local-dev-conf.json" 
    }; 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function(_configService_, $httpBackend, $rootScope) { 
    scope = $rootScope.$new(); 

    configService = _configService_; 
    httpBackend = $httpBackend; 
    // $rootScope.configObj = tstConfigObj; 

    spyOn(configService, 'getConfig').and.callThrough(); 

    httpBackend.when('GET', 'conf.json').respond(200, tstConfig); 
    httpBackend.when('GET', 'local-dev-conf.json').respond(200, {}); 

    })); 

    describe('Function getConfig():', function() { 

    it('should check if it was called', inject(function() { 
     configService.getConfig(); 
     httpBackend.flush(); 

     expect(configService.getConfig).toHaveBeenCalled(); 
    })); 

    it('should check if statement', inject(function() { 
     scope.configObj = "Tesla is awesome"; 
     results = scope.configObj; 
     configService.getConfig(); 
     httpBackend.flush(); 
     expect(results).not.toBe(null); 
    })); 
    console.log(results); 
    }); 
}); 

私はconfigObj != nullを作成する必要がありますが、そうするのに苦労。私の範囲が間違っているかもしれない? coverage

EDIT:機能にconfigObjを渡すことによって修正:ここ

は私のカバレッジだ

getConfig: function(configObj) {

答えて

0

あなたconfigObj変数がローカルであるので、あなたはscope.configObj = "Something"を呼び出すときには、変数を変更しませんあなたは変更したい。それを変数としてreturn文に入れてみてください。

+0

お返事ありがとうございます。私はあなたが直接上にあるreturn文の後にconfigObjを定義することを意味すると思いますか?もしそうなら、それは残念ながらエラーを投げます – FF5Ninja

+0

これは動作しませんか? 'return { configObj:null、 //あなたの他のコード' – Jelle