How to use webmock regex matcher?

2019-06-14 21:52发布

How to match a URL like:

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")

3条回答
beautiful°
2楼-- · 2019-06-14 22:00

The second argument to stub_request must be a regular expression, not a string.

stub_request(:post, /http:\/\/www.example.com\/foo\/\d+\/bar/)
查看更多
看我几分像从前
3楼-- · 2019-06-14 22:05

You can use %r{} instead of // for your regular expression in Ruby to avoid having to escape the forward slashes in URLs. For example:

stub_request(:post, %r{\Ahttp://www.example.com/foo/\d+/bar\z})
查看更多
地球回转人心会变
4楼-- · 2019-06-14 22:08

http://www\..*?\.com/foo/\d+/bar should work for you.

查看更多
登录 后发表回答