简体   繁体   English

如何开始使用摩卡咖啡并进行首次测试?

[英]How can I get started with mocha and make my first test?

I want to figure out whats wrong with my JS code. 我想弄清楚我的JS代码有什么问题。 The code is pretty simple, and has a really simple error, but I want to set up tests. 代码很简单,并且有一个非常简单的错误,但是我想设置测试。 I googled around for javascript testing frameworks and found one similar to RSPEC, called Mocha, which has a Chai extension which I think gives it more jQuery functionality. 我到处搜索javascript测试框架,发现一个类似于RSPEC的名为Mocha,它具有Chai扩展名,我认为它具有更多的jQuery功能。

I'm not a nodejs user, I'm usually a PHP backend with some Ruby on Rails experience, So npm install mocha intimidates me. 我不是nodejs用户,我通常是具有一些Ruby on Rails经验的PHP后端,因此npm install mocha吓倒了我。 I couldn't find anything on google that suggested I could get mocha set up without node package manager, and it generally seemed really hard to set up my first test. 我在Google上找不到任何建议没有节点软件包管理器就可以设置摩卡的东西,而且通常很难进行我的第一个测试。 I don't want to spend so much time familiarizing myself with node, when all I want to do is write a simple test and try testing out. 当我只想编写一个简单的测试并尝试进行测试时,我不想花太多时间来熟悉节点。

Actually, "installing" mocha is not that much more complicated than "installing" jQuery! 实际上,“安装” mocha并不比“安装” jQuery复杂得多! Simply include the file. 只需包含文件。 This makes it actually really easy to set up a JSFiddle for testing Javascript code. 这实际上使得设置用于测试Javascript代码的JSFiddle真的非常容易。 I've done that with 3 simple tests so that you can see the syntax, and easily get started testing your javascript code with mocha. 我已经通过3个简单的测试完成了此操作,以便您可以查看语法,并轻松开始使用mocha测试您的javascript代码。 As always, refer to the mocha docs , the chai docs when you want to learn more. 与往常一样,当您想了解更多信息时,请参考mocha文档chai文档

mocha.setup("bdd");
chai.should();
var asdf = "tests.";
//simple test
describe('Simple ' + asdf, function(){
    it('true = true?', function(){
        var j = true;
        var i = true;
        i.should.equal(j);
    });
});
// Run all our test suites.  Only necessary in the browser.
mocha.run();

Here is a JSFiddle with mocha loaded so you can get started: 这是一个加载了Mocha的JSFiddle,因此您可以开始使用:

http://jsfiddle.net/kbjwLsab/12/ http://jsfiddle.net/kbjwLsab/12/

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

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