在 Rails 咖啡脚本中,我有这个点击处理程序:
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-result。这在浏览器中运行良好,但不适用于黄瓜测试:
@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 件商品”#features/step_definitions/web_steps.rb:24 预计会有内容“您的购物车中有 1 件商品”在 [...]
您的购物车中有0 件商品\n 查看您的购物车 [ ...]
(RSpec::Expectations::ExpectationNotMetError)
步骤“我等待 ajax 请求完成”是:
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
有什么建议么?