How to use webmock regex matcher?

两盒软妹~` 提交于 2019-12-06 16:44:38

问题


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


回答1:


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




回答2:


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



回答3:


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

stub_request(:post, /http:\/\/www.example.com\/foo\/\d+\/bar/)


来源:https://stackoverflow.com/questions/13773895/how-to-use-webmock-regex-matcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!