gpt4 book ai didi

ember.js - 你如何测试你的 emberjs 路由?

转载 作者:行者123 更新时间:2023-12-04 21:53:29 25 4
gpt4 key购买 nike

在几个月没有看 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 ?

最佳答案

这是完整的测试,使用 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();
});
});
});
});

希望能帮助到你。

关于ember.js - 你如何测试你的 emberjs 路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11432896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com