【问题标题】:Meteor js, iron-router, unit testing with Route.go('...') in server side doesn't workMeteor js,iron-router,在服务器端使用 Route.go('...') 进行单元测试不起作用
【发布时间】:2015-04-22 18:16:12
【问题描述】:

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

【问题讨论】:

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


    【解决方案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 (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 () {
             ...
          });
        });
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 2013-12-09
      • 2016-08-26
      • 2015-02-27
      • 2013-09-03
      • 2016-05-09
      • 1970-01-01
      相关资源
      最近更新 更多