【发布时间】:2014-05-14 16:52:47
【问题描述】:
我正在尝试测试来咖啡脚本类,但我遇到了 ajax 调用问题。例如在咖啡中,我使用 $.getJSON 从服务器获取一些数据。如何在我的测试中捕获此请求或重定向到某个假服务器?我读过一些关于 sinon fakeServer 的文章,我尝试过这样的事情:
describe "TestClass", ->
describe "#run", ->
beforeEach ->
url = "/someUrl'
@server = sinon.fakeServer.create()
$ =>
@server.respondWith("GET", url,
[200, {"Content-Type": "application/json"},
'{}'])
@entriesDownloader = new TestClass().run()
但它不起作用。在方法运行中,我使用 jquery 调用 API。如何捕捉这个请求并返回一些模拟。谢谢大家的回答。
【问题讨论】:
标签: testing coffeescript sinon