0

此方法重定向到外部网站。我如何测试这种行为?我知道该方法可以正常工作,因为我在浏览器中进行了测试,但我无法通过测试。

  def create
    if @order.save
      redirect_to Gateway.new(@order).send
    else
      render 'new'
    end
  end

我写了这个测试失败,出现“缺少模板错误”:

describe 'User creates an order with valid info' do
  before do
    WebMock.allow_net_connect!
    stub_request(:any, "https://external-site.com/v2/checkout")
  end

  it 'creates the order and redirects to checkout page' do
    fill_in_order
    expect { click_button "Submit" }.to change(Order, :count)
  end
end

错误:

Failure/Error: click_button "Submit"
     ActionView::MissingTemplate:
       Missing template orders/create, ...
4

1 回答 1

0

将响应设置为返回示例:

Replaying raw responses recorded with curl -is

`curl -is www.example.com > /tmp/example_curl_-is_output.txt`
raw_response_file = File.new("/tmp/example_curl_-is_output.txt")

from file

stub_request(:get, "www.example.com").to_return(raw_response_file)

or string

stub_request(:get, "www.example.com").to_return(raw_response_file.read)
于 2013-05-22T13:48:47.303 回答