2016-10-28 52 views
0

我正在使用摩卡在Meteor应用程序中测试某些窗体。应用程序中的路由已通过身份验证,因此只有登录的用户或具有“管理员”角色的用户才能查看它们。使用黑猩猩/摩卡测试流星应用程序 - 自动登录以测试已验证的路线

当测试打开浏览器查看URL并填写表单时,它将按照预期重定向到登录页面。

有没有办法在测试前自动登录用户,所以我不必删除路由认证?

这里的测试代码到目前为止

describe('Create a Client', function() { 
    it('should create a new client @watch', function() { 
     browser.url('http://localhost:3000/dashboard/clients/new') 

     [...] 

    }); 
}); 

回答

1

使用本:

function login(user) { 
    browser.url('http://localhost:3000') 
    browser.executeAsync(function(user, done) { 
    Meteor.loginWithPassword(user.username, user.password, done) 
    }, user) 

} 

// now you can do this: 
login({ 
    username: 'someone', 
    password: 'aSecret' 
}); 
browser.url('http://localhost:3000/dashboard/clients/new') 

请注意,您需要确保用户至上存在,为此,你可以使用固定装置。

这里看到更多的信息: https://forums.meteor.com/t/solved-how-can-i-wait-for-before-hooks-to-finish-when-testing-with-chimp-meteor-cucumber/18356/12

+0

这是真棒,谢谢! – Sean

+0

快速的问题,我完全按照您的指示实施了代码。我已更改用户名/密码,但第二个“browser.url”功能不起作用。用户登录后,应用程序停留在仪表板上,测试失败。任何想法为什么? – Sean

相关问题