我尝试验证单击提交按钮时是否执行了正确的表单操作。我在 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
,所以看起来好像已经完成了一些请求,但假服务器无法捕获它?
最佳答案
这不能工作,因为假服务器只适用于 ajax 请求。假服务器所做的唯一事情就是模拟 XmlHttpRequest 对象并调用传递的成功/错误函数。因此,在您的情况下,单击按钮将导致页面重新加载,并显示新的帖子网址。
关于javascript - 使用 sinon 和 jasmine 测试提交时的表单操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15656945/