javascript - 使用 sinon 和 jasmine 测试提交时的表单操作

标签 javascript coffeescript jasmine sinon

我尝试验证单击提交按钮时是否执行了正确的表单操作。我在 Chrome 中使用 Karma(以前称为 testaulous)运行测试,并使用 sinon 创建一个假服务器来捕获 POST 请求。看起来没有被捕获

表格

<form action="/authenticate_open_id" method="POST">
<label>Or sign up with:</label>
<input type="hidden" name="openid_identifier" value="https://www.google.com/accounts/o8/id"/>
<input type="hidden" name="return_url" value="/"/>
<input id="signup_with_google" type="submit" value="Google Account"/></form><

测试代码

server = sinon.fakeServer.create()
server.autoRespond = true
server.respondWith 'POST', '/authenticate_open_id', [302, {}, '']

view.$el.find('#signup_with_google').click()
waitsFor ->
    server.requests.length > 0
, 'server has not been called', 100                    
runs ->
    console.log 'server', server.requests

假服务器永远不会捕获该请求(但类似的代码适用于 ajax 发起的请求)。但是我在运行测试套件的 Chrome 浏览器中遇到错误

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:9876/authenticate_open_id

,所以看起来好像已经完成了一些请求,但假服务器无法捕获它?

enter image description here

最佳答案

这不能工作,因为假服务器只适用于 ajax 请求。假服务器所做的唯一事情就是模拟 XmlHttpRequest 对象并调用传递的成功/错误函数。因此,在您的情况下,单击按钮将导致页面重新加载,并显示新的帖子网址。

关于javascript - 使用 sinon 和 jasmine 测试提交时的表单操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15656945/

相关文章:

javascript - jQuery 属性选择器问题

javascript - 使用 Jasmine 进行异步测试

javascript - 如何为 HTML5 视频设置音频延迟(不同步)

用于创建 Svelte 元素而不是模板的 JavaScript API

apache - shell 脚本中的 CoffeeScript —— 由 Apache 运行(作为 CGI)

javascript - Atom编辑器:用于插入时间戳的代码段

jasmine - Karma 在 Karma 开始时挂起

asp.net - 如何为现有 Web 应用程序创建功能/UI 测试

javascript - HERE map 标记与光标指针

shell - 这些 shell 转义字符是什么?