我的orders_controller
需要将订单转发到支付网关。它使我的测试失败:没有路由匹配 [GET]“/v2/checkout/payment.html”
这是 PaymentGateway 对象重定向到的 URL。如何欺骗我的测试,使其认为支付网关返回了响应?事实上,事实并非如此。它可能会也可能不会返回用户,具体取决于他的选择。这与使用 Paypal 付款类似。
def create
@order = current_user.orders.build(params[:order])
@order.add_line_items_from_cart(current_cart)
if @order.save
destroy_cart current_cart
payment = PaymentGateway.new(@order).send
redirect_to payment
else
render 'new'
end
end
feature 'User creates an order with valid info' do
background do
setup_omniauth_user
visit root_path
create(:line_item, cart: Cart.last)
click_link 'cart-link'
click_link 'th-checkout-link'
end
scenario 'an order is created and cart is deleted' do
cart = Cart.last
fill_in_order
expect {
click_button "Submit"
}.to change(Order, :count)
cart.reload.should be_nil
end
end
User creates an order with valid info an order is created and cart is deleted
Failure/Error: click_button "Submit"
ActionController::RoutingError:
No route matches [GET] "/v2/checkout/payment.html"
# ./spec/features/orders_spec.rb:64:in `block (3 levels) in <top (required)>'
# ./spec/features/orders_spec.rb:63:in `block (2 levels) in <top (required)>'
最佳答案
您可以使用 gem,例如 WebMock stub 并设置远程 HTTP 请求的期望。
我用它来模拟支付网关和单点登录身份验证。这是语法使用的示例,尽管您显然需要更新主体以反射(reflect)应返回的内容。
stub_request(:any, "http://example.com/v2/checkout/payment.html").to_return(
:body => "SUCCESS",
:status => 200,
:headers => { 'Content-Length' => 3 }
)
关于ruby-on-rails - 如何模拟重定向到外部 API 的 Controller 的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16523753/