简体   繁体   English

如何在Sails中使用mocha测试控制器?

[英]How to test controller using mocha in Sails?

I have the following Controller for my login page: 我的登录页面有以下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;

I am using Mocha as my test framework. 我使用Mocha作为我的测试框架。 The application is based on Sails . 该应用程序基于Sails How would I write Mocha test cases and run them on the provided Controller ? 我如何编写Mocha测试用例并在提供的Controller上运行它们?

I'm using supertest to call controllers as a user, to do so, first lift the sails in the before function so it can be used as the server by supertest. 我正在使用supertest作为用户调用控制器,为此,首先解除之前函数中的风帆,以便它可以被supertest用作服务器。

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

Then initialize supertest with the url of your sails app 然后使用你的sails app的url初始化supertest

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

You can now write test to post / get data to test your controller from frontend as would a client. 您现在可以编写测试来发布/获取数据以从前端测试您的控制器,就像客户端一样。



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();
    });
}

I think the main thing is to include sails application into your testcase. 我认为主要的是将sails应用程序包含在您的测试用例中。 I also found no examples with controllers tests but some with models: 我也没有发现控制器测试的例子,但有些模型:

Is it possible to use the Mocha Framework against a model in Sails.js? 是否可以对Sails.js中的模型使用Mocha Framework?

Cannot unit test my model in sailsjs 无法在sailsjs中对我的模型进行单元测试

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

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