2016-06-20 107 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

編輯:在函數傳遞configObj修正:

getConfig: function(configObj) {

回答

0

你configObj變量是本地的,所以當你調用scope.configObj = "Something"它不會改變的變量你想改變。嘗試把它作爲一個變量放在return語句中。

+0

嘿,謝謝你的回覆。我假設你是指在直接在上面的return語句之後定義configObj?如果是這樣,不幸的是會拋出一個錯誤 – FF5Ninja

+0

這是行不通的? 'return { configObj:null, // your your other code' – Jelle