简体   繁体   English

如何测试单元测试基于jQuery的代码?

[英]How can I test unit test my jQuery-based code?

I have following code in a file named index.js. 我在名为index.js的文件中有以下代码。 I want to test some functions in it add , print being the prime one. 我想测试一些功能在里面addprint是首要的。

(function($){
  "use strict";

  $(function(){

    var add = function(a, b){
    // ...
      },
    print = function(str){
    // ...
      },
      setup = function(){
    // ...
      },
      init = function(){
    // ...
      };

      setup();
      init();
  });
})(jQuery);

How can I do so? 我该怎么办? Does this way of coding add any security on client side? 这种编码方式是否会增加客户端的安全性? All I have is code that runs on the client side. 我所拥有的只是在客户端运行的代码。 No server involvement what-so-ever. 完全没有服务器参与。

I tried : 我试过了 :

var outerObj = (function(greet){
    var innerObj = (function(){
        return {test: function(){ console.log(greet); }}
    })();
    return innerObj;
})("hi");

outerObj.test();

but, in my case, innerObj line has a $ on the right hand of equal sign, making console yell error that shouts $(...) is not a function . 但是,在我的情况下, innerObj行在等号的右侧有一个$ ,使控制台大喊错误,喊$(...)不是函数 Which I agree, it's an array of single document object. 我同意,它是单个文档对象的数组。

Check out var a = (function(){ var val = $(function(){return 10;})(); })(); 签出var a = (function(){ var val = $(function(){return 10;})(); })(); in any jquery enabled web-page's console, for the error. 在任何启用了jquery的网页的控制台中查找错误。

Even if I ditch outer shell and bring $(function() {}); 即使我抛弃外壳并带来$(function() {}); out. 出来。 How am I gonna test that? 我要如何测试?

It's still an object, still an error. 它仍然是一个对象,仍然是一个错误。

What kind of testing can I perform, unit, bdd, etc. and how? 我可以执行什么样的测试,单元测试,bdd测试等等,以及如何进行?

I don't quite see what pattern you're using here - it seems like a bit of an anti-pattern. 我不太清楚您在这里使用什么模式-似乎有点反模式。 It's not testable, and doesn't expose any behaviour for real world use. 它是不可测试的,并且不会暴露任何可用于现实世界的行为。 I'd highly recommend reading (or at least skimming) Javascript Design Patterns by Addy Osmani, available for free. 我强烈建议阅读(或至少浏览)Addy Osmani的Javascript Design Patterns (免费)。

For Unit Testing, I've always found a happy midpoint between the simple constructor pattern and revealing module pattern easiest to work with. 对于单元测试,我总是在最简单的构造函数模式和显示模块模式之间找到一个快乐的中点。

var x = function($){
"use strict";

  var add = function(a, b){
     alert('add');
    },
    print = function(str){
     return 1;
    },
    setup = function(){
      alert('setup');
    },
    init = function(){
      alert('init');
    };

  init();
  setup();

  return {
      add: add,
      print: print
  };
};

var y = new x(jQuery);

In the above example, y will have the add and print methods available. 在上面的示例中, y将具有可用的addprint方法。 Check out a sample test on this fiddle . 这个小提琴上查看样本测试。

As a side note, I've recently been using the excellent QUnit framework for setting up and running my JS Unit Tests. 附带说明一下,我最近一直在使用出色的QUnit框架来设置和运行JS单元测试。 It's by the jQuery Team, so you know it's gonna be good ;). 它是由jQuery团队提供的,所以您知道它会很好;)。 I'd also suggest checking out BlanketJS for reporting on coverage, and qunit-parameterize for setting up multiple test cases. 我还建议您检查BlanketJS以报告覆盖率,并使用qunit-parameterize设置多个测试用例。

For what its worth, BDD isn't a 'test type', but a way of working. 就其价值而言,BDD不是“测试类型”,而是一种工作方式。 For what tests you should have - Unit Tests and Integration Tests are the most important 2, IMO. 您应该进行哪些测试-单元测试和集成测试是最重要的2,IMO。 I use QUnit for Unit Tests, and Selenium with NUnit for Integration Testing. 我将QUnit用于单元测试,将Selenium与NUnit用于集成测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM