javascript - 如何在 Sails 中使用 mocha 测试 Controller ?

标签 javascript node.js unit-testing mocha.js sails.js

我的登录页面有以下 Controller:

// Authentication Controller
// the basics of Passport.js to work.
var AuthController = {

    // localhost:1337/login  Render the login page
    // <form role="form" action="/auth/local" method="post">
    //     <input type="text" name="identifier" placeholder="Username or Email">
    //     <input type="password" name="password" placeholder="Password">
    //     <button type="submit">Sign in</button>
    // </form>

    login: function(req, res) {
        var strategies = sails.config.passport,
            providers = {};

        // Get a list of available providers for use in templates.
        Object.keys(strategies).forEach(function(key) {
            if (key === 'local') return;
            providers[key] = {
                name: strategies[key].name,
                slug: key
            };
        });

        // Render the `auth/login.ext` view
        res.view({
            providers: providers,
            errors: req.flash('error')
        });

    },

    // Log out a user and return them to the homepage
    // Passport exposes a logout() function on req (also aliased as logOut()) that
    // can be called from any route handler which needs to terminate a login
    // session. Invoking logout() will remove the req.user property and clear the
    // login session (if any).
    logout: function(req, res) {
        req.logout();
        res.redirect('/login');
    },

    // The registration form is Just like the login form
    register: function(req, res) {
        res.view({
            errors: req.flash('error')
        });
    },

    // Create a third-party authentication endpoint
    provider: function(req, res) {
        passport.endpoint(req, res);
    },

    // Create a authentication callback endpoint
    // This endpoint handles everything related to creating and verifying Pass-
    // ports and users, both locally and from third-aprty providers.
    // Passport exposes a login() function on req (also aliased as logIn()) that
    // can be used to establish a login session. When the login operation
    // completes, user will be assigned to req.user.
    callback: function(req, res) {
        passport.callback(req, res, function(err, user) {
            req.login(user, function(err) {
                // If an error was thrown, redirect the user to the login which should
                // take care of rendering the error messages.
                if (err) {
                    res.redirect('/login');
                }
                // Upon successful login, send the user to the homepage were req.user
                // will available.
                else {
                    res.redirect('/');
                }
            });
        });
    }
};

module.exports = AuthController;

我正在使用 Mocha作为我的测试框架。该应用程序基于Sails . 我将如何编写 Mocha 测试用例并在提供的 Controller 上运行它们?

最佳答案

我正在使用 supertest 以用户身份调用 Controller ,为此,首先在 before 函数中扬起风 sails ,以便它可以被 supertest 用作服务器。

before(function (done) {
 Sails.lift({
        // configuration for testing purposes
        log: {
            //level: 'silly'
            level: 'silent'
        },
        hooks: {
            grunt: false,
        },
    }, function (err, sails) {
done(err, sails);
        });
}

然后使用您的 sails 应用程序的 url 初始化 supertest

request = require('supertest');
agent = request.agent(appurl);

您现在可以编写测试来发布/获取数据,以像客户端一样从前端测试您的 Controller 。



it('should do the post and return whatever', function (done) {
    agent.post('/controller/function')
    .send(the_post_object_with_params)
    .end(function (err, res) {
        if (err) {
            console.log(err);
        }
        console.log(res.body); // the content 
        done();
    });
}

关于javascript - 如何在 Sails 中使用 mocha 测试 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689201/

相关文章:

javascript - removeclass addclass 多个选择器

node.js - 如何使用同一端口为不同的 Node.js 应用程序提供服务?

c# - 单元测试扩展方法

node.js - 如何缩小 node.js 应用程序? (服务器端 node.js,而不是客户端 javascript)

node.js - 运行 npm install 时显示请求

javascript - Jest 比较具有任意属性值的对象

unit-testing - Golang mockgen "does not implement"接口(interface)问题

javascript - Visual Studio 2010 不会在 javascript 的断点处中断

javascript - 我在 jquery datepicker 上的 "beforeshowday"函数不起作用

javascript - 如果网页未加载,则显示评论/信息框的 Ajax 代码