【问题标题】:Testing form action on submit with sinon and jasmine使用 sinon 和 jasmine 测试提交时的表单操作
【发布时间】:2013-03-17 10:07:07
【问题描述】:

我尝试验证单击提交按钮时是否执行了正确的表单操作。我在 Chrome 中使用 Karma(以前称为 testacular)运行测试,并使用 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

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

【问题讨论】:

    标签: javascript coffeescript jasmine sinon


    【解决方案1】:

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

    【讨论】:

    • 谢谢。这就说得通了。我猜这种类型的表单操作不是那么可测试的。
    猜你喜欢
    • 2012-08-10
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多