Ember.js 你如何测试你的路线?

Ember.js 你如何测试你的路线?,ember.js,ember-old-router,Ember.js,Ember Old Router,在几个月没有看emberjs之后,我现在尝试回到它,因此我正在尝试新的路由器。我想测试一下我的路线 有人试过用emberjs写一些路由测试吗 让我们假设以下最基本的路由器: App.Router = Ember.Router.extend({ root: Ember.Route.extend({ index: Ember.Route.extend({ route: '/', connectOutlets: function(router, context) {

在几个月没有看emberjs之后,我现在尝试回到它,因此我正在尝试新的路由器。我想测试一下我的路线

有人试过用emberjs写一些路由测试吗

让我们假设以下最基本的路由器:

App.Router = Ember.Router.extend({
  root: Ember.Route.extend({
    index: Ember.Route.extend({
      route: '/',
      connectOutlets: function(router, context) {
        router.get('applicationController').connectOutlet({name: 'home'});
      }
    })
  })
})

如何测试加载
root.index
路由是否正确加载
HomeView

以下是完整的测试,使用&:

代码:


希望有帮助。

以下是Ember为您测试connectOutlet的方法:


其他与路由相关的测试可以在

中找到,但是检查路由是否已定义是不够的。我需要检查在加载路由时是否呈现了适当的视图。我刚刚用完整的测试代码更新了答案。基本上,您只想测试您的路由是否使用正确的参数调用“connectOutlet”函数,接下来发生的是Ember的责任。如果您正在进行单元测试,那么您希望尽可能地进行隔离测试,这意味着与路由之外的其他组件隔离。谢谢,但是我无法从它的实例中获取视图的名称。我要检查的是是否渲染了适当的视图。不仅仅是something.view instanceof TestApp.PostView显示该视图是PostView的实例,对于视图的其余细节,您可以直接使用变量视图进行检查。“从其实例获取视图名称”的确切含义是什么?
instanceof
正是我想要的\o/:)
describe("Given the Router", function(){

    var router = null;

    beforeEach(function(){
        router = Router.create();
    });

    afterEach(function(){
        router = null;
    });

    it("Should be defined", function(){
        expect(router).toBeDefined();
    });

    it("Should have an root route", function(){
        expect(router.get("root")).toBeDefined();
    });

    describe("its root route", function(){
        var root = null;
        beforeEach(function(){
            root = router.get("root").create();
        });

        afterEach(function(){
            root = null;
        });

        it("should have an index route", function(){
            expect(root.get("index")).toBeDefined();
        });

        describe("its index route", function(){
            var indexRoute = null;
            beforeEach(function(){
                indexRoute = root.get("index").create();
            });

            it ("should have route of /", function(){
                expect(indexRoute.get("route")).toEqual("/");
            });

            it ("should connect the outlets to home", function(){

                var fakeRouter = Em.Object.create({applicationController: {connectOutlet: function(){} } });

                var connectOutletSpy = sinon.spy(fakeRouter.applicationController, "connectOutlet");

                var methodCall = connectOutletSpy.withArgs({name:"home"});

                indexRoute.connectOutlets(fakeRouter);

                expect(methodCall.calledOnce).toBeTruthy();
            });
        });
    });
});
test("connectOutlet instantiates a view, controller, and connects them", function() {
  var postController = Ember.Controller.create();

  var appController = TestApp.ApplicationController.create({
    controllers: { postController: postController },
    namespace: { PostView: TestApp.PostView }
  });
  var view = appController.connectOutlet('post');

  ok(view instanceof TestApp.PostView, "the view is an instance of PostView");
  equal(view.get('controller'), postController, "the controller is looked up on the parent's controllers hash");
  equal(appController.get('view'), view, "the app controller's view is set");
});