如何用茉莉花BDD创建存根为Ajax功能 [英] How to create Stub for ajax function using Jasmine BDD

查看:113
本文介绍了如何用茉莉花BDD创建存根为Ajax功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力寻找任何实例如何伪造一个Ajax调用使用茉莉花BDD?

I'm struggling to find any examples on how to fake an ajax call using Jasmine BDD?

我有一个自定义的AJAX功能的工作原理像这样...

I have a custom ajax function that works like so...

ajax({
    url: 'JSON.php',
    dataType: 'json',           
    onSuccess: function(resp) {
        console.log(resp);
    }
});

...和我不知道如何创建一个存根假调用实际的AJAX功能。

...and I've no idea how to create a stub to fake calling the actual ajax function.

我想避免调用Ajax的功能,就好像一个真正的Ajax调用服务器需要一些时间来作出回应,我已经规范的负载在我的测试套件就可以减缓我的测试套件。

I want to avoid calling the ajax function as it could slow down my test suite if a real ajax call to the server takes some time to respond and I've loads of specs in my test suite.

我听说,你可以使用 spyOn(命名空间,'阿贾克斯')但就是讨厌直线距离,因为它需要我总结我的在对象AJAX 函数只是使用 spyOn 功能(但不管我是不是能够跟着,因为我无法找到任何具体的例子来伪造一个AJAX调用)。

I've heard that you can use spyOn(namespace, 'ajax') but that is annoying straight away as it requires me to wrap my ajax function in an object just to use the spyOn function (but regardless I wasn't able to follow along as I couldn't find any specific examples to fake an ajax call).

我还听说,你可以使用 createSpy()但同样的文件是不是非常有帮助的(无论是在GitHub上相应的wiki)。

I've also heard that you can use createSpy() but again the documentation isn't very helpful (neither is the corresponding wiki on GitHub).

任何帮助解释如何使用间谍来创建一个假的Ajax调用将大大AP preciated!

Any help explaining how to use spies to create a fake ajax call would be greatly appreciated!

推荐答案

您可以使用SinonJS嘲讽的框架,它有一个内置的的假冒服务器。您可以轻松地使用它与茉莉:

You can use SinonJS mocking framework, which has a build in fake server. You can easily use it with jasmine:

beforeEach(function() {
        server = sinon.fakeServer.create();
        server.respondWith([200, { "Content-Type": "text/html", "Content-Length": 2 }, "OK"])
});

顺便说一句。如果你的AJAX功能是在全局命名空间为什么不叫 spyOn(窗口,'阿贾克斯')

这篇关于如何用茉莉花BDD创建存根为Ajax功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆