1

我正在编写一个仅使用基于 Facebook 的 PassportJS 身份验证的应用程序。部分提供的 API 仅在用户通过身份验证时可用。我正在尝试找到一种方法来测试这部分 API。我发现了许多测试经过身份验证的 API 的示例,其中使用了登录名+密码,但这里不是这种情况。我也找到了这个线程,但不幸的是,我不明白答案有什么帮助。

你们海军有什么例子可以解释如何实现这一目标吗?

假设我有一个 API:

app.get('/users/me, users.me);

此调用要求用户通过 Facebook 进行身份验证。

我如何使用超级测试进行测试并使用 FB 模拟用户身份验证?

4

1 回答 1

1

You can use zombie + mocha to test this ....Zombie opens up a virtual browser fron tthe backend (which you dont see ) and clicks on any link on the page and then test this result by asserting using mocha . login to facebook before you start the application and then the link which fb authentication and assert for the new route(because you would have success redirection url) using zombie . Lets say the redirection url after successful login is /auth/facebook/success and /auth/facebook be the url where the login is tested . HERe is the code that could achieve it

var assert = require('assert'),
    virtualBrowser = require('zombie');
describe('facebbok Login test', function () {
    describe('Checking whether login is successfull based o nthe redirection URL',         function () {
        it('Checking for the sucess URL', function (done) {
            virtualBrowser.visit('http://127.0.0.1:3000/auth/facebook', function (err, browser) {
                if (err)
                    throw err;
               console.log('Zombie visited the page,page loaded    ');
               assert.equal(browser.location.pathname, '/auth/facebook/success');

                //all your tests goes here

                /*you'll be using this 'browser object in the callback for further tests */

               done();
            });
        });
    });
});

Run the test by running it against the mocha (will be present inside the node_module/mocha of the folder of the installed module ) ~/node_modules/mocha/bin/mocha ./zombie-test/test_sign_facebook.js If mocha reports timeout since the login might be slow , just add more threshhold test time by using -t parameter ~/node_modules/mocha/bin/mocha -t 12000 ./zombie-test/test_sign

于 2014-06-22T18:20:00.750 回答