使用ajax和json测试问题,甚至使用ajax等待解决方法
在Rails coffee脚本中,我有一个单击处理程序:使用ajax和json测试问题,甚至使用ajax等待解决方法,ajax,json,cucumber,Ajax,Json,Cucumber,在Rails coffee脚本中,我有一个单击处理程序: addProductToCartHandler = ()-> $('#add_to_cart').click((event)-> $.ajax({ type: 'POST', url: '/line_items.json' data: {product_id:product_id} success: (d
addProductToCartHandler = ()->
$('#add_to_cart').click((event)->
$.ajax({
type: 'POST',
url: '/line_items.json'
data: {product_id:product_id}
success: (data)->
$('.carts_number_of_items').html(data.number_of_items)
$('#carts_explanation').effect("pulsate",{times:2}, 200)
error: ()->
});
event.preventDefault()
return false
)
此脚本通过$.ajax调用以下操作:
def create
[...]
format.json{
@cart.save ?
( render json: {number_of_items:@cart.line_items.sum('quantity') } ) :
( render json: { error: t("line_items.error") } )
}
它计算购物车中的商品数量,并返回json结果这在浏览器中运行良好,但在cucumber test中不起作用:
@javascript
Scenario: Add a product to a cart via ajax control
And I follow "Cucumbers"
When I follow "Add To Cart"
And I wait for the ajax request to finish
Then I should see "1 item in your cart"
这与本报告不符:
然后我会看到“购物车中的1件物品”#功能/step_定义/web_步骤。rb:24
应在[…]中包含内容“购物车中的1项”
0购物车中的项目\n查看购物车[…](RSpec::Expections::ExpectationNotMetError) “我等待ajax请求完成”的步骤是:
当(/^I等待ajax请求完成$/)时
开始时间=time.now
page.evaluate_脚本('jQuery.isReady&&jQuery.active==0')。
class.u不应为eql(字符串)
直到page.evaluate_脚本('jQuery.isReady&&jQuery.active==0')或
(开始时间+5秒)
有什么建议吗 我敢肯定,从18个月前开始,你就已经明白了这一点,对于有类似问题的偷窥者来说:
- 由于某种原因出错(在这种情况下,您可以使用“保存”和“打开”页面检查内容,或查看page.driver.console\u消息以查看是否存在JS错误)
- 耗时超过5秒(在这种情况下,您可以将5.5秒的时间更改为更大的时间,以查看是否有效)
- jQuery别名不工作?(我看到您正在使用$-jQuery.isReady&&jQuery.active==0返回控制台吗?)
When(/^I wait for the ajax request to finish$/) do
start_time = Time.now
page.evaluate_script( 'jQuery.isReady&&jQuery.active==0' ).
class.should_not eql(String)
until page.evaluate_script('jQuery.isReady&&jQuery.active==0') or
(start_time + 5.seconds) < Time.now do
sleep 1
end
end