javascript - 在 Jasmine/Node.js 中测试独立的 JS 文件

标签 javascript node.js jasmine karma-jasmine karma-runner

我在 node.js 应用程序中有一个独立的 javascript 文件 cacheBustingInterceptor.js。它是工厂模式中的一项服务,当应用程序运行时由 app.js 调用。

/** 
* Intercept the http request 
* If the config.url is from upload or templates and is a html file
* append the cacheBusting Param
* If the template url has query param exisitng
* append &_dt=epochtime else append ?_dt=epochtime
*/
var cacheBustingInterceptor = {
CacheBustingService: CacheBustingServiceFactory
};

function CacheBustingServiceFactory() {
function CacheBustingService() {}

var possibleHtmlPaths = ['templates', 'upload'];
CacheBustingService.prototype.appendCacheBustingParam = function(templateUrl) {
    for (var index = 0; index != possibleHtmlPaths.length; index++) {

        // check if the url has is .html and from upload and templates
        var addCacheBusting = templateUrl.indexOf(possibleHtmlPaths[index]) != - 1 && 
            templateUrl.indexOf('.html') != - 1;
        var hasQueryParams = templateUrl.indexOf('?') != -1;

        if (addCacheBusting) {
            if (hasQueryParams) {
                return templateUrl + window.appendCacheBustingParam;
            } else {
                return templateUrl + window.cacheBustingParam;
            }
        } 
    }
    return templateUrl;
};

CacheBustingService.prototype.interceptRequest = function() {
    var _this = this;
    var cacheBuster = {
        request: function (config) {
            config.url = _this.appendCacheBustingParam(config.url);
            return config;
        }
    }
    return cacheBuster;
}

return CacheBustingService;

我们调用它的方式是在配置中向 app.js 添加注入(inject)器并将工厂推送到配置。

像这样,

   app. config([''$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('templateCacheBustingInjector');

  app.factory('templateCacheBustingInjector', ['$injector', function 
    ($injector) {
    var CacheBustingService = 
         $injector.invoke(cacheBustingInterceptor.CacheBustingService);
         var cacheBustingService = new CacheBustingService();

    return cacheBustingService.interceptRequest();
  }]);

现在一切正常,但我想在 cacheBustingInterceptor.js 中对方法“appendCacheBustingParam”进行单元测试,并用完了从 jasmine 单元测试中调用它的想法

事情累了: 1.调用我在app.js中调用的方式,但是我得到服务注入(inject)错误或提供者错误 2. 使用 require 加载 js 文件,但不支持 require,我尝试使用 browsify,但是,这也无济于事。

  require('../main/webapp/media/scripts/cacheBustingInterceptor.js'); 

  fdescribe('Cache Busting Service', function() {
      var cacheBustingService;
       var $injector;

beforeEach((inject(function (_$injector_) {
    $injector = _$injector_;
   $injector.get(cacheBustingInterceptor.CacheBustingService);
    // var CacheBustingService = $injector.invoke(cacheBustingInterceptor.CacheBustingService);
})));

it('Test appendCacheBustingParam', function() {
    cacheBustingService = new CacheBustingService();
    spyOn(cacheBustingService.prototype, 'appendCacheBustingParam');
    expect(cacheBustingService.prototype).toHaveBeenCalled();
});

});

最佳答案

当您说“独立”时,我认为这里存在术语问题。

单元 测试一个独立的代码单元。

集成 测试测试 独立单元之间的交互。

您的方法的单元测试可能类似于

const mock = {}; // fill with appropriate properties
const result = CacheBustingService
  .prototype
  .appendCacheBustingParam
  .call(mock, someURL);

expect(result).toBe(expectedResult);

单元测试不应要求连接服务。单元测试通常应该可以在 Node 中从命令行运行,理想情况下甚至不需要像 npm install 这样运行,因为你已经把所有东西都 stub 了。您的单元测试应该在亚秒级运行。最好适用于整个套件,但这可能无法实现,具体取决于代码库大小。

听起来您想要的是一个集成测试:创建服务是否真的正确连接了所有内容?

在集成测试的情况下,实际上构建服务来找出答案是完全有意义的,这意味着在浏览器中运行它,例如 karma 和实际的依赖关系(不是作为独立文件)。集成测试可能会变慢/运行频率降低。

最后一点

您的方法不太适合自动化测试,因为它引用全局变量并通过窗口执行:

        if (hasQueryParams) {
            return templateUrl + window.appendCacheBustingParam;
        } else {
            return templateUrl + window.cacheBustingParam;
        }

改为将这些参数传递给方法。

关于javascript - 在 Jasmine/Node.js 中测试独立的 JS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54755888/

相关文章:

javascript - iOS 的 NativeScript livesync 在启动时失败

node.js - Mocha/Chai 中的断言/期望无法捕获构造函数中的抛出错误

node.js - 在 Intern 中调用 Chai 插件返回错误

node.js - 使用 Nodejs 和 body-parser 发布表单数据

具有 ControlValueAccessor 测试的 Angular 2(4) 组件

Javascript:如何加载 "kill"对象?

javascript - 如何检查数据是否写入firebase数据库?

angularjs - 如何使用 Jasmine 测试 AngularJS 可信 html?

javascript - ECMAScript 6 中更短的类初始化

angularjs - getText() 的返回不能与字符串进行比较