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

时间:2015-02-20 22:54:38

标签: unit-testing meteor velocity publish-subscribe iron-router

我需要去一个特定的路由器来测试发布行为,我正在使用铁路由器包,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中,我已经尝试在我的路由器控制器中调用一个方法,但也不起作用,是否有人知道它可能是什么?请

1 个答案:

答案 0 :(得分:1)

Iron路由器是一个客户端/服务器库,可以通过客户端测试轻松测试。

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

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

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 (!(typeof MochaWeb === 'undefined')) {

  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 () {
         ...
      });
    });
  });
}