【问题标题】:Authentication testing with Meteor使用 Meteor 进行身份验证测试
【发布时间】:2015-04-03 19:34:17
【问题描述】:

我正在编写需要身份验证的流星测试,但遇到了一系列问题。 这是我的代码:

MochaWeb?.testOnly ->
  describe "Login", ->
    describe "security", ->
    it 'should take you to /login/ if you are not logged in', ->
      Meteor.flush()
      chai.assert.equal Router.current().url, '/login/'

    it 'should allow logins and then take us to /', ->
      Meteor.flush()
      Accounts.createUser username: 'test', password: 'test'
      Meteor.loginWithPassword 'test', 'test', (err) ->
        console.log err
        chai.expect(err).to.be undefined
        chai.assert.equal Router.current().url, '/'
  1. 我的测试通过了,即使我收到控制台消息,例如 Exception in delivery result of invoking 'login': TypeError: object is not a function

  2. 我的 console.log 调用给了我

    {error: 403, reason: "User not found", details: undefined, message: "User not found [403]", errorType: "Meteor.Error"…}

在控制台上,速度日志窗口上什么都没有

  1. 我的用户未按预期进行身份验证。一个原因可能是我的应用程序没有帐户密码包,因为我不想要它(我只希望谷歌应用程序用户能够登录)。不过,我想要一种简单的方法来处理流星测试中的身份验证,因为我的大多数测试都涉及经过身份验证的用户。
  2. 我不确定 assert equal 是否有效,或者我必须设置某种超时来等待重定向。在这种情况下,这不是问题,但是我是否必须将我拥有的每个测试方法都嵌套在 loginWithPassword 中?我会觉得这有点不舒服。

非常感谢任何帮助和建议!

最好的,

【问题讨论】:

  • 嘿,您可以在哪里使测试成功,谢谢
  • 我对任何解决方案都不太信服,这就是为什么我还没有尝试过它们的原因。我真的认为应该有一种方法可以让登录状态不受干扰并专注于实际测试,就像成熟的框架一样。与此同时,我试图避免在 Meteor 中进行这种测试。

标签: testing meteor mocha.js velocity


【解决方案1】:

您确定您已经创建了用户吗?我认为你应该在你的 fixture.js 中创建它。

【讨论】:

    【解决方案2】:
    MochaWeb.testOnly(function() {
        describe("Client", function() {
            describe("firstTest", function() {
                // Meteor.users.remove({});
                before(function(done) {
                    Accounts.createUser(currentUser, function(err, success) {
                        Meteor.loginWithPassword(currentUser, function(err) {
                            console.log("This works");
                            // done(); 
                        });
                    });
                });
            });
        });
    });
    

    这是源文件,我在其中实现了相同的 https://github.com/trinisofttechnologies/mocha-test/blob/master/tests/mocha/client/client.coffee

    这是代码所在的仓库 https://github.com/trinisofttechnologies/mocha-test

    【讨论】:

    • 我认为这仍然很尴尬,但最好的答案。也许我们可以把它放到一个通用函数中,这样我们就可以用 before(create_user_and_signin) 结束,然后继续我们的测试(和一般生活)?这将接近 Rails 最方便的登录助手
    • 不确定你的要求是什么,但对我来说这是要求,因此,我根据这个做了测试用例。让我知道您的情况,以便我可以为您提供准确的测试代码。
    • 我不是要测试帐户系统本身,而是要尽可能轻松地进行身份验证,以便测试假定经过身份验证的用户的方法。
    • hmm,你在这里得到了错误的印象,我在这里也想达到同样的效果,我也想测试方法,发布需要登录帮助。
    • loginWithPassword 函数已更改签名。 docs.meteor.com/api/accounts.html#Meteor-loginWithPassword
    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2017-05-13
    • 1970-01-01
    相关资源
    最近更新 更多