我正在使用混合版本的摩卡,提摩卡,来构建一个基于钛SDK的应用程序的单元测试。我对BDD和mocha完全陌生,所以API学习曲线非常陡峭。这是我的问题,一个剥离的测试线束。我可以访问index.js模块、它的方法和属性。但是,我不知道如何访问该模块中的命名函数,在本例中是doClick()。
我用的是摩卡+ should.js。在下面的测试工具中,上下文"index“通过,但是上下文"doClick”失败。我对这个API太陌生了,所以我不确定我是否正确地提出了这个问题。如何访问模块中的函数?
index-mocha.js
// creates the "mocha" global necessary to run a test suite anywhere in your app
var should = require('should');
module.exports = function(index) {
// create the test suite
describe('mochatest', function() {
context('index', function() {
it('index exists', function() {
should.exist(index);
});
it('index.open function', function() {
should(index.open).be.a.Function;
});
it('id = index', function() {
index.id.should.equal('index');
});
});
context('doClick', function() {
it('doClick exists', function() {
should.exist(index.doClick);
// return;
});
it('doClick is a function', function() {
should(index.doClick).be.a.Function;
});
});
});
var outputFile = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, 'results.json');
outputFile.createFile();
mocha.setup({
reporter: 'ti-spec', // the reporter to use with your tests
outputFile: outputFile, // write results to the given Ti.Filesystem.File file
quiet: false // if true, suppress all console logging
});
// run the tests
mocha.run();
};index.js
function doClick(e) {
alert($.label.text);
}
if(runTests){
require('ti-mocha');
$.index.addEventListener('open', function(){
require('index-mocha')($.index);
});
}
$.index.open();发布于 2015-06-04 22:26:41
很高兴看到有人在玩钛的测试:)
为了澄清这一点,变量$引用了当前控制器的一个实例。另外,合金提供了对视图元素的引用,您已经通过这个变量为其定义了一个id;这可能被看作是一个小糖,因为所有这些视图都可以通过$.getViews()访问。
因此,您的控制器文件中定义的所有函数只能从该控制器中访问。如果您希望从外部访问它们,最简单和最干净的方法是将它们导出。
这可以通过两种方式实现:
$.doClick = doClick;exports变量
exports.doClick = doClick;结果将与编译过程中的合金与控制器a.k.a $合并exports变量(最初仅为空对象)完全相同。
然后,只需通过require而不是index视图传递控制器,就可以访问视图和新添加的侦听器。
https://stackoverflow.com/questions/30650921
复制相似问题