我不确定我是否采用正确的方法进行测试?

Template.home.events({
    "click div#u-home-nav > button": function (event) {
    "use strict";
    var where = $(event.currentTarget).data("target").split("/")[1];

    if (where === "overview" || where === "frameworks" || where === "dashboard" || where === "dummy") {
        _.each(places, function (value) {
            if (value.main === where) {
                $("span#u-current-place").text(value.title);
            }
        });

        Router.go(where);
    }
});


我尝试遵循Location reload in Jasmine中的解决方案
但是我得到以下内容:
错误:找不到命名为undefined的路由

describe("click routing", function () {
    "use strict";
    var element = $("div#u-home-nav.btn-group").find("button[data-target='/overview']")

    function goPage() {
        Router.go("/overview")
    }

    function bindEvents() {
        element.click(goPage);
    }

    describe('when the button is clicked', function () {
        var button;
        var handlers;
        beforeEach(function () {
            handlers = {
                location: Router.go(), // handle for Router.go()
                goPage: goPage         // handle for your goPage()
            };

            button = element.click(goPage);

            // attach Spy on goPage() and let the function call through
            spyOn(handlers, 'goPage').and.callThrough();

            // attach Spy on Router.go()
            spyOn(handlers, 'location');

        });

        it('will execute goPage function', function () {
            button.trigger('click');
            expect(handlers.goPage).toHaveBeenCalled();
        });

        it('will go to the page', function () {
            button.trigger('click');
            expect(handlers.location).toHaveBeenCalled();
        });

        afterEach(function () {
            // clean up event bindings after each test
            button.off('click');
        });

    });
});


任何建议都很好!

最佳答案

尝试这个:

describe("click routing", function () {
    "use strict";
    var element = $("div#u-home-nav.btn-group").find("button[data-target='/overview']");

    function goPage() {
        Router.go("/overview")
    }

    function bindEvents() {
        element.click(goPage);
    }

    describe('when the button is clicked', function () {
        var button;
        var handlers;
        beforeEach(function () {
            handlers = {
                location: Router.go(), // handle for Router.go()
                goPage: goPage         // handle for your goPage()
            };

            button = element;
            element.click(goPage);
            element.click();

            // attach Spy on goPage() and let the function call through
            spyOn(handlers, 'goPage').and.callThrough();

            // attach Spy on Router.go()
            spyOn(handlers, 'location');

        });

        it('will execute goPage function', function () {
            expect(handlers.goPage).toHaveBeenCalled();
        });

        it('will go to the page', function () {
            expect(handlers.location).toHaveBeenCalled();
        });

        afterEach(function () {
            // clean up event bindings after each test
            button.off('click');
        });

    });
});

关于javascript - 在 meteor Jasmine 中测试router.go的点击事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29904768/

10-09 08:13
查看更多