Skip to content

Commit d79667c

Browse files
committed
snapshot tests for show_body_diff setting
1 parent 0b7cd6f commit d79667c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

tests/testthat/_snaps/Adapter.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# show_body_diff configuration setting
2+
3+
Code
4+
POST("https://hb.opencpu.org/post", body = list(apple = "red"))
5+
Condition
6+
Error:
7+
! Real HTTP connections are disabled.
8+
! Unregistered request:
9+
i POST: https://hb.opencpu.org/post with body {apple: red} with headers {Accept: application/json, text/xml, application/xml, */*}
10+
11+
You can stub this request with the following snippet:
12+
stub_request('post', uri = 'https://hb.opencpu.org/post') %>%
13+
wi_th(
14+
headers = list('Accept' = 'application/json, text/xml, application/xml, */*'),
15+
body = list(apple="red")
16+
)
17+
18+
registered request stubs:
19+
GET: https://hb.opencpu.org/post with body {"apple":"green"}
20+
21+
22+
Body diff:
23+
< stub$body > request_s..
24+
@@ 1,3 @@ @@ 1,3 @@
25+
$apple $apple
26+
< [1] "green" > [1] "red"
27+
28+
29+
============================================================
30+
31+
# show_body_diff configuration setting - > 1 stub
32+
33+
Code
34+
POST("https://hb.opencpu.org/post", body = list(apple = "red"))
35+
Condition
36+
Error:
37+
! Real HTTP connections are disabled.
38+
! Unregistered request:
39+
i POST: https://hb.opencpu.org/post with body {apple: red} with headers {Accept: application/json, text/xml, application/xml, */*}
40+
41+
You can stub this request with the following snippet:
42+
stub_request('post', uri = 'https://hb.opencpu.org/post') %>%
43+
wi_th(
44+
headers = list('Accept' = 'application/json, text/xml, application/xml, */*'),
45+
body = list(apple="red")
46+
)
47+
48+
registered request stubs:
49+
GET: https://hb.opencpu.org/post with body {"apple":"green"}
50+
GET: https://hb.opencpu.org/post with body {"apple":"green"}
51+
GET: https://hb.opencpu.org/post with body {"pear":"purple"}
52+
53+
54+
Body diff:
55+
i diffs: >1 stub found, showing diff with least differences
56+
< stub$body > request_s..
57+
@@ 1,3 @@ @@ 1,3 @@
58+
$apple $apple
59+
< [1] "green" > [1] "red"
60+
61+
62+
============================================================
63+

tests/testthat/test-Adapter.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,37 @@ test_that("Adapter initialize method errors as expected", {
1616
)
1717
expect_error(adap$new(), "should not be called directly")
1818
})
19+
20+
test_that("show_body_diff configuration setting", {
21+
webmockr_configure(show_body_diff = TRUE)
22+
withr::defer(webmockr_configure(show_body_diff = FALSE))
23+
24+
library(httr, warn.conflicts = FALSE)
25+
enable(adapter = "httr")
26+
27+
stub_request("get", "https://hb.opencpu.org/post") %>%
28+
wi_th(body = list(apple = "green"))
29+
30+
expect_snapshot(
31+
POST("https://hb.opencpu.org/post", body = list(apple = "red")),
32+
error = TRUE
33+
)
34+
})
35+
36+
test_that("show_body_diff configuration setting - > 1 stub", {
37+
webmockr_configure(show_body_diff = TRUE)
38+
withr::defer(webmockr_configure(show_body_diff = FALSE))
39+
40+
library(httr, warn.conflicts = FALSE)
41+
enable(adapter = "httr")
42+
43+
stub_request("get", "https://hb.opencpu.org/post") %>%
44+
wi_th(body = list(apple = "green"))
45+
stub_request("get", "https://hb.opencpu.org/post") %>%
46+
wi_th(body = list(pear = "purple"))
47+
48+
expect_snapshot(
49+
POST("https://hb.opencpu.org/post", body = list(apple = "red")),
50+
error = TRUE
51+
)
52+
})

0 commit comments

Comments
 (0)