你如何测试你的emberjs路线?

Dam*_*IEU 4 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) {
        router.get('applicationController').connectOutlet({name: 'home'});
      }
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

你如何测试加载root.index路线正确加载HomeView

Sha*_*.io 12

这是使用Jasmine&Sinon的完整测试:

码:

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();
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.