简体   繁体   English

使用Mocha / Sinon测试NodeJ-Sails

[英]Testing NodeJs with Mocha/Sinon - Sails

I'm relatively new to nodejs and sailsjs framework, and I really enjoy write code with it, but coming from a PHP background I found testing nodeJs a bit weird. 我对nodejs和sailsjs框架比较陌生,我非常喜欢用它编写代码,但是来自PHP背景,我发现测试nodeJs有点奇怪。

I'm trying to test with mocha and sinon the method login of my AuthService , but being not expert I would need an example on how do you achieve a successful test for it , unfortunately documentation online is still a bit poor an example will be a lots useful thanks! 我试图来测试mochasinon的方法login我的AuthService ,但作为不是高手,我需要对你如何实现它成功的测试为例,不幸的是在线文档仍然是一个有点差一个例子是非常有用的感谢!

login: function(username,password,callback) {

        User.findOneByUsername(username, function(err,user){

            // If has some error
            if (err) { return callback(err) }

            // if the user is not found with that username
            if (!user) { return callback(err,false); }

            // if is found we match the password
            bcrypt.compare(password, user.password, function (err, res) {

                if (!res || err) return callback(err,false);

                return callback(err,true,user);
            });
        });
    }

If you can make light on this will be really appreciated 如果您能对此有所了解,将不胜感激

Check we.js example for how to setup you tests: 查看we.js示例以了解如何设置测试:

link: https://github.com/wejs/we-example/tree/master/test ( with only "headless browser" tests now ) 链接: https : //github.com/wejs/we-example/tree/master/test (现在仅具有“无头浏览器”测试)

For integration and unit tests with supertest check: https://github.com/wejs/we/tree/emberjsDev/tests 对于带有supertest的集成和单元测试,请检查: https : //github.com/wejs/we/tree/emberjsDev/tests

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

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