call-expectations: Expectation: does the given call match the expected?

Description Usage Arguments Details Examples

Description

Together with mock can be used to verify whether the call expression (expect_call) and/or argument values (expect_args) match the expected.

Usage

1
2
3
4
5
expect_call(mock_object, n, expected_call)

expect_args(mock_object, n, ...)

expect_called(mock_object, n)

Arguments

mock_object

A mock object.

n

Call number or total number of calls.

expected_call

Expected call expression; will be compared unevaluated.

...

Arguments as passed in a call.

Details

With expect_called you can check how many times has the mock object been called.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
library(testthat)

# expect call expression (signature)
m <- mock()
with_mock(summary = m, summary(iris))

# it has been called once
expect_called(m, 1)

# the first (and only) call's arguments matche `summary(iris)`
expect_call(m, 1, summary(iris))

# expect argument value
m <- mock()
a <- iris
with_mock(summary = m, summary(object = a))
expect_args(m, 1, object = a)
# is an equivalent to ...
expect_equal(mock_args(m)[[1]], list(object = a))

lbartnik/mock documentation built on May 20, 2019, 8:27 p.m.