使用 Chimp/Mocha 测试 Meteor 应用程序 - 自动登录以测试经过身份验证的路由
Testing Meteor application with Chimp/Mocha - automatic login to test authenticated routes
我正在使用 Mocha 测试 Meteor 应用程序中的某些表单。应用程序中的路由经过身份验证,因此只有登录用户或具有 'administrator' 角色的用户才能查看它们。
当测试打开浏览器查看 url 并填写表单时,它会按预期重定向到登录页面。
有没有办法在进行测试之前自动登录用户,这样我就不必删除路由身份验证?
这是目前的测试代码
describe( 'Create a Client', function() {
it( 'should create a new client @watch', function() {
browser.url('http://localhost:3000/dashboard/clients/new')
[...]
});
});
使用这个:
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')
请注意,您需要先确保用户存在,为此您可以使用固定装置。
我正在使用 Mocha 测试 Meteor 应用程序中的某些表单。应用程序中的路由经过身份验证,因此只有登录用户或具有 'administrator' 角色的用户才能查看它们。
当测试打开浏览器查看 url 并填写表单时,它会按预期重定向到登录页面。
有没有办法在进行测试之前自动登录用户,这样我就不必删除路由身份验证?
这是目前的测试代码
describe( 'Create a Client', function() {
it( 'should create a new client @watch', function() {
browser.url('http://localhost:3000/dashboard/clients/new')
[...]
});
});
使用这个:
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')
请注意,您需要先确保用户存在,为此您可以使用固定装置。