diff --git a/README.md b/README.md index 91735e0..20c9d63 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_666 TILs and counting..._ +_667 TILs and counting..._ --- @@ -523,6 +523,7 @@ _666 TILs and counting..._ - [Last Raised Exception In The Call Stack](ruby/last-raised-exception-in-the-call-stack.md) - [Limit Split](ruby/limit-split.md) - [Listing Local Variables](ruby/listing-local-variables.md) +- [Mocking Requests With Partial URIs Using Regex](ruby/mocking-requests-with-partial-uris-using-regex.md) - [Navigate Back In The Browser With Capybara](ruby/navigate-back-in-the-browser-with-capybara.md) - [Next And Previous Floats](ruby/next-and-previous-floats.md) - [Or Operator Precedence](ruby/or-operator-precedence.md) diff --git a/ruby/mocking-requests-with-partial-uris-using-regex.md b/ruby/mocking-requests-with-partial-uris-using-regex.md new file mode 100644 index 0000000..acf02ae --- /dev/null +++ b/ruby/mocking-requests-with-partial-uris-using-regex.md @@ -0,0 +1,21 @@ +# Mocking Requests With Partial URIs Using Regex + +Generally when mocking out requests with the +[webmock](https://github.com/bblimke/webmock) gem, we specify full request +URIs like so: + +```ruby +stub_request(:post, 'http://localhost:4000/api/posts') +``` + +We may not want to specify the entire URI though. For instance, the host may +change or be configurable. The `stub_request` method allows us to use regex. + +```ruby +stub_request(:post, %r|/api/posts|) +``` + +Using the `%r` regex literal syntax, we are able to avoid escaping all of +the `/` characters in our URI. + +h/t Brian Dunn