【问题标题】:Testing with meteor and jasmine: Iron Router Routing does not work as expected使用流星和茉莉花进行测试:Iron Router Routing 无法按预期工作
【发布时间】:2015-09-03 04:36:27
【问题描述】:

我正在尝试使用速度/茉莉花框架为流星应用程序编写测试。

我的老板想要进行 UI 测试(端到端),所以我需要为用户界面编写测试。

我现在遇到了如何通过应用程序测试正常导航的问题。例如,我测试用户注册程序的想法是这样的:

describe 'Login and Usermanagement System', ->
    it 'should say the user is logged out when no user is logged in', ->
        # This test Works
        expect(Meteor.user()).toBeFalsy()

    it 'should show a welcome screen if the user is logged out', ->
        currentUrl = Router.current().location.get().href;
        routeName = Router.current().route.getName();

        # This test Works as the startpage in our app (When you hit /) is always system.welcome as long as you are not logged in.
        expect(routeName).toBe("system.welcome")

    it 'should show a register screen if the user is logged out and clicked on register', (done) ->
        Router.go("/register")
        routeName = Router.current().route.getName()

        # This test does not work as the Router.go seems to be async.
        expect(routeName).toBe("system.register")

我的问题是第三次测试。当路由加载完成下一件事情时,我需要某种回调。我可以等待 2 秒左右,但这会不必要地减慢我的测试速度。

是否有 Router.go(route, options, callback) 这样的东西或者我怎样才能得到这样的行为?

我们使用的技术:MeteorJS 和 Iron Router 进行路由,Velocity Test Framework 和 Jasmine 进行测试。

【问题讨论】:

    标签: meteor jasmine iron-router


    【解决方案1】:

    您需要使用文档Integration Tests With Iron Router中描述的帮助器

    其中规定: 根据您要使用的模式,将此帮助程序保存到 tests/jasmine/client/integration/lib/wait_for_router_helper.jstests/jasmine/client/unit/_wait_for_router_helper.js

    (function (Meteor, Tracker, Router) {
      var isRouterReady = false;
      var callbacks = [];
    
      window.waitForRouter = function (callback) {
        if (isRouterReady) {
          callback();
        } else {
          callbacks.push(callback);
        }
      };
    
      Router.onAfterAction(function () {
        if (!isRouterReady && this.ready()) {
          Tracker.afterFlush(function () {
            isRouterReady = true;
            callbacks.forEach(function (callback) {
              callback();
            });
            callbacks = []
          })
        }
      });
    
      Router.onRerun(function () {
        isRouterReady = false;
        this.next();
      });
    
      Router.onStop(function () {
        isRouterReady = false;
        if (this.next) {
          this.next();
        }
      });
    })(Meteor, Tracker, Router);
    

    然后你在你的测试中像这样使用它:

    describe('My Spec', function () {
      beforeEach(function (done) {
        Router.go('/myPage');
        Tracker.afterFlush(done);
      });
    
      beforeEach(waitForRouter);
    
      it('should do something', function () {
        // Your test
      });
    });
    

    【讨论】:

    • 如何在“it”块内等待路由器?我需要浏览多个路线来测试一些东西(例如,进入登录页面,创建用户,更改密码,然后检查密码是否被更改)
    • 也不确定那个,而且肯定在我的名单上,但我还不能这样做。您可能必须创建另一个包装函数来调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 2015-11-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多