Meteor js,铁路由器,在服务器端使用 Route.go('...') 进行单元测试不起作用

Meteor js, iron-router, unit testing with Route.go('...') in server side doesn't work

我需要去一个特定的路由器来测试发布行为,我正在使用 iron 路由器包,Router.go('...'),但在服务器端不起作用,它抛出我有一个错误:

   //XXX this assumes no other routers on the parent stack which we should probably fix      // 14
   I20150220-18:47:00.749(-4)?     router.dispatch(req.url, {                                                                // 15
   I20150220-18:47:00.750(-4)?       request: req,                                                                           // 16
   I20150220-18:47:00.750(-4)?       response: res                                                                           // 17
   I20150220-18:47:00.750(-4)?     }, next);                                                                                 // 18
   I20150220-18:47:00.750(-4)?   } has no method 'go'
   I20150220-18:47:00.750(-4)?     at packages/velocity:test-proxy/tests/mocha/server/publish/folders-publisher-tests.js:23:1
   I20150220-18:47:00.750(-4)?     at wrappedFunc (packages/mike:mocha/server.js:204:1)
   I20150220-18:47:00.751(-4)?     at runWithEnvironment (packages/mike:mocha/server.js:156:1)

我的路由器文件在 lib 中,我已经尝试在我的路由器控制器中调用一个方法,但也不起作用,有人知道它可能是什么吗????请

Iron router 是一个 client/server 库,很容易通过客户端测试进行测试。

这就是我使用 Meteor + Velocity + Mocha + chai 进行测试的方式:

首先,您需要创建一个文件 _lib.js,其中包含一个函数,该函数将在加载路由器和您的模板时等待(我在那里找到的想法:

this.afterRendered = function(template, f) {
  var cb = template.rendered;
  template.rendered = function() {
    if (typeof cb === "function") {
      cb();
    }
    template.rendered = cb;
    if (typeof f === "function") {
      f();
    }
  }
}

现在您可以写下您的 clientTest.js

if (Mochaweb) {
  MochaWeb.testOnly(function () {
    describe("what you test", function () {
      // If the argument done is specified, it means that you have 
      // something async in your test : afterRendered, setTimeout, setInterval ...
      it("Should do something", function (done) { 
        try {
          Router.go("user.login"); // which will load the page localhost/user/login and not localhost/user.login

          afterRendered(Template.userlogin, function() {
            chai.expect($("#something:visible")[0]).to.not.be.undefined;
            $("#magical-button").click();
            setTimeout(function() {
                chai.expect($("magical-text:visible").html()).to.be.equal("I can handle async process");
                done();
            },100);
          })
        } catch (e) {
          done(e);
        }
      });
      it("will test something else after the previous test, function () {
         ...
      });
    });
  });
}