1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Mocking Requests With Partial URIs Using Regex as a ruby til

This commit is contained in:
jbranchaud
2018-04-17 14:47:50 -05:00
parent 5de41a9000
commit 0711b6ec00
2 changed files with 23 additions and 1 deletions

View File

@@ -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