簡體   English   中英

測試if語句— AngularJS / Jasmine

[英]Testing an if statement — AngularJS/Jasmine

嘗試訪問此if語句,但是我的報道是我沒有。 有什么想法嗎? 提前致謝!

'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 ,但是要這樣做。 我的范圍可能有問題嗎?

這是我的報道: 覆蓋范圍

編輯:通過在函數中傳遞configObj進行修復:

getConfig: function(configObj) {

您的configObj變量是本地變量,因此,當您調用scope.configObj = "Something"它不會更改您要更改的變量。 嘗試將其作為變量放入return語句中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM