茉莉花和主干和js模板

发布于 2024-12-23 04:30:11 字数 391 浏览 1 评论 0原文

你好,我正在尝试使用 jasmine headless webkit: http://johnbintz.github.com/ jasmine-headless-webkit/

我一直在我的应用程序的主 HTML 文件中嵌入 js 模板。我的应用程序开始变得相当复杂,我想我可以使用 jasmine 来给它更多的测试覆盖率。由于我的视图引用了主 HTML 文件中的 javascript 模板,因此我的视图将出错。有什么办法可以解决这个问题吗?我可以以某种方式重现我的应用程序的 HTML 文件的状态吗?即,加载其所有 js 模板,并在头部以正确的顺序加载供应商 javascript?

Hi, I'm attempting to use jasmine headless webkit: http://johnbintz.github.com/jasmine-headless-webkit/

I've been embedding js templates inside of my app's main HTML file. My app is starting to get rather complex and I thought I could use jasmine to give it some more test coverage. Since my views reference javascript templates that are in my main HTML file, my views will error out. Is there some way to get around this? Can I somehow reproduce the state of my app's HTML file? Ie, load all its js templates, and load the vendor javascripts in the correct order in the head?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野の 2024-12-30 04:30:12

您可以在 jasmine 规范中加载 html 固定装置。请参阅: https://github.com/velesin/jasmine-jquery

loadFixtures('myfixture.html');
$('#my-fixture').myTestedPlugin();
expect($('#my-fixture')).to...;

无论如何,你可以检查我的示例使用backbone.js和jasmine的项目: https://github.com/lucassus/tdd-with-backbonejs 并检查我如何在现实生活中使用这种技术。
另外,我将写一篇关于用 jasmine 测试骨干观点的博客文章: http://blog.bandzarewicz.com

You can load html fixtures in your jasmine specs. See: https://github.com/velesin/jasmine-jquery

loadFixtures('myfixture.html');
$('#my-fixture').myTestedPlugin();
expect($('#my-fixture')).to...;

Anyway you can check my sample project with backbone.js and jasmine: https://github.com/lucassus/tdd-with-backbonejs and check how I'm using this technique in the real life.
Also I'm going to write blog post about testing backbone's views with jasmine: http://blog.bandzarewicz.com

剪不断理还乱 2024-12-30 04:30:12

当然,您可以将模板文件添加到 jasmine 测试运行器页面。

另一个解决方案是模拟视图 DOM 元素。您可以在构造函数中插入元素,例如 new View({el: $($('myTemplate').html())})。这样做,您可以在测试中添加元素模拟或间谍,而无需基于模板创建 DOM 元素:

beforeEach(function() {
    el = {bind: function(){}};
    spyOn(el, 'bind');
    var OrderListView = new OrderSumView({el: el, model: model});
});

it('it should render the sum after firing the remove event', function() {

    //test that bind('click', someFunction) was called to your element
    expect(el.bind).toHaveBeenCalledWith('click', model.bind.argsForCall[0][1]);

    // fire up the function that was bind to the click event
    el.bind.argsForCall[0][1]() 
})

使用此解决方案,您可以在大多数情况下完全模拟 html。这是我们测试大型 GWT 应用程序的方式。使用GWT 比使用backbone 容易得多,因为我们处于JAVA 世界,但原理是相同的。不要测试 DOM,只需测试您的业务逻辑。如果对我的私人骨干项目这样做的话,效果会很好。

另请看一下这个SO: 存根 jQuery 选择器调用?

Sure you can add your template files to your jasmine test runner page.

Another solution would be to mock out your views DOM element. You could insert the element in the constructor like new View({el: $($('myTemplate').html())}). Doing so, you can add an element mock or spy in your test, with no need to create DOM element based on your template:

beforeEach(function() {
    el = {bind: function(){}};
    spyOn(el, 'bind');
    var OrderListView = new OrderSumView({el: el, model: model});
});

it('it should render the sum after firing the remove event', function() {

    //test that bind('click', someFunction) was called to your element
    expect(el.bind).toHaveBeenCalledWith('click', model.bind.argsForCall[0][1]);

    // fire up the function that was bind to the click event
    el.bind.argsForCall[0][1]() 
})

With this solution you can totally mock out html in the most cases. Its the way we test our large GWT application. Its much easier with GWT than with backbone cause we are in the JAVA world, but the principle is the same. Dont test the DOM, just test your business logic. If done it this way for my private backbone project and it works well.

Take also a look at this SO: Stub out a jQuery selector call?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文