【问题标题】:Testing an if statement -- AngularJS/Jasmine测试 if 语句——AngularJS/Jasmine
【发布时间】:2023-03-25 18:40:01
【问题描述】:

试图访问这个 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) {

【问题讨论】:

    标签: javascript angularjs unit-testing jasmine code-coverage


    【解决方案1】:

    您的 configObj 变量是本地变量,因此当您调用 scope.configObj = "Something" 时,它不会更改您要更改的变量。尝试将其作为变量放入 return 语句中。

    【讨论】:

    • 您好,感谢您的回复。我假设您的意思是在直接在上面的 return 语句之后定义 configObj ?如果是这样,不幸的是它会抛出一个错误
    • 这不起作用吗? return { configObj: null, // your other code
    猜你喜欢
    • 1970-01-01
    • 2018-10-31
    • 2012-09-21
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    相关资源
    最近更新 更多