2012-07-24 37 views
2

您好所有我是新来的JavaScript和我被困在一个问题使用昆特framework.But我的功能的单元测试工作,需要专家功能不可见的Qunit

帮助这里是我的问题

我有一个文件名为calc.js这样

var calc; // global variable 
(function(){ 
    calc = (function(){ 
     function calc(container, options) { 
      this._container = container; 
      this._jq_container = $(container); 
      this._options = options || {}; 
      this.setupDefaults(); 

     } 
     calc.prototype.setupDefaults = function() { 
      var _self = this; 
      _self._options.type = _self._options.type ? _self._options.type : 'add'; 
     }; 
     calc.prototype.add = function (val1, val2) { 
      return val1 + val2; 
     }; 
     calc.prototype.sub = function (val1, val2) { 
      return val1 - val2; 
     }; 
    return calc; 
    })(); 
    $.fn.calc = function(options){ 
    this.each(function(){ 
     return new calc(this, options); 
    }); 
    }; 
})(); 

现在我有qunit的HTML文件中像这样

<!DOCTYPE html> 
<html> 
<head> 
    <title>QUnit Test Suite</title> 
     <link rel="stylesheet" href="qunit/qunit.css"> 
    <script src="qunit/qunit.js"></script> 
     <script src="http://code.jquery.com/jquery-1.7.2.js" type="text/javascript"></script> 
    <script type="test/javascript" src="calc.js"></script> 
<script> 
    $(document).ready(function(){ 

test('add function test', function() { 

equal(calc.add(2,4),"6" ,"function working correctly"); 
/*I have also tried to access add function using some other method but alway got an error*/ 
}); 

}); 
</script> 
</head> 
<body> 
    <h1 id="qunit-header">QUnit Test Suite</h1> 
    <h2 id="qunit-banner"></h2> 
    <div id="qunit-testrunner-toolbar"></div> 
    <h2 id="qunit-userAgent"></h2> 
    <ol id="qunit-tests"></ol> 
</body> 
</html> 
一些事情

我的两个HTML和calc.js都在同一个文件 当我运行这个文件,我得到的钙没有定义

下我的立场不作为的原因我已计算的全球性的错误。 我也使用window.calc = calc但一切都是徒劳 任何一个可以请指导我怎么能在我的HTML测试文件 任何帮助将不胜感激

+0

忽略这个..的QUnit方面,你甚至可以从一个脚本块调用calc函数吗? – Evildonald 2012-08-02 22:38:46

回答

0

访问这些功能我跑你的代码块jsfiddle.net并试图您的函数调用calc.add(2,4);没有运行。

我将电话更改为calc.prototype.add(2,4),我可以按照预期调用您的功能。我建议你添加.prototype到你的QUnit测试?