28

如何匹配如下 URL:

http://www.example.com/foo/:id/bar
http://www.example.com/foo/1/bar
http://www.example.com/foo/999/bar

stub_request(:post, "www.example.com")

4

3 回答 3

36

您可以在 Ruby 中使用%r{}而不是//您的正则表达式,以避免必须转义 URL 中的正斜杠。例如:

stub_request(:post, %r{\Ahttp://www.example.com/foo/\d+/bar\z})
于 2014-09-13T15:17:44.523 回答
20

stub_request 的第二个参数必须是正则表达式,而不是字符串。

stub_request(:post, /http:\/\/www.example.com\/foo\/\d+\/bar/)
于 2014-02-17T15:31:53.440 回答
12

http://www\..*?\.com/foo/\d+/bar应该为你工作。

于 2012-12-09T12:24:54.247 回答