Lies can be harmless

Lies can be necessary

Lies exist on a spectrum

Plot Twist: Python 🐍

A.K.A. Lying to Your CodeWhat is Mocking?

What is Mocking?

Mocking is a testing technique that requires lying to your code, on purpose, to control the ecosystem it lives in and depends on.

Worked with These Before?

Two Questions to Ask


What do we need to lie about?
&
Where do we need to lie about it?

Well, it dependsHow can we Mock?

How do we even do this?

October 32nd? Sure, Why Not 😎

A simple, but fragile, example



was_dose_taken_today <- function(dose_date) {
  dose_date == Sys.Date()
}

A simple, but fragile, example



was_dose_taken_today <- function(dose_date) {
  dose_date == Sys.Date()
}

library(testthat)
test_that("logs dose correctly", {
  dose_date <- as.Date("2026-09-16")
  expect_true(was_dose_taken_today(dose_date))
})
Test passed 🎉

A simple, but fragile, example



was_dose_taken_today <- function(dose_date) {
  dose_date == Sys.Date()
}

library(testthat)
test_that("logs dose correctly", {
  dose_date <- as.Date("2026-09-16")
  expect_true(was_dose_taken_today(dose_date))
})
Test failed
`was_dose_taken_today(dose_date)` is not TRUE

`actual`:   FALSE
`expected`: TRUE

A simple, but fragile, example



was_dose_taken_today <- function(dose_date) {
  dose_date == Sys.Date()
}

library(testthat)
test_that("logs dose correctly", {
  local_mocked_bindings(
    Sys.Date = function() as.Date("2026-09-16"),
    .package = "base"
  )
  dose_date <- as.Date("2026-09-16")
  expect_true(was_dose_taken_today(dose_date))
})

Test passed with 1 success 🎉.

A simple, but fragile, example



was_dose_taken_today <- function(dose_date) {
  dose_date == Sys.Date()
}

library(testthat)
test_that("logs dose correctly", {
  local_mocked_bindings(
    Sys.Date = function() as.Date("2026-09-16"),
    .package = "base"
  )
  dose_date <- as.Date("2026-09-16")
  expect_true(was_dose_taken_today(dose_date))
})

Test passed with 1 success 🎉.

Plot Twist: Python 🐍

The First Time I Had to Mock




The First Time I Had to Mock

Same Concept, Different Syntax



@pytest.mark.parametrize(
    "library, tbl_type",
    [("Polars", "polars"), ("Pandas", "pandas"), ("Ibis", "duckdb")],
)
def test_get_dataframe_report_missing_libraries(library, tbl_type):
    validation = Validate(data="small_table")

    with patch("pointblank.validate._is_lib_present") as mock_is_lib:
        mock_is_lib.return_value = False  # library not present

        with pytest.raises(
            ImportError, match=f"The {library} library is not installed"
        ):
            validation.get_dataframe_report(tbl_type)

Same Concept, Different Syntax



from unittest.mock import patch

with patch("pointblank.validate._is_lib_present") as mock:
    mock.return_value = False
    validation.get_dataframe_report("polars")

Same Concept, Different Syntax



from unittest.mock import patch

with patch("pointblank.validate._is_lib_present") as mock:
    mock.return_value = False
    validation.get_dataframe_report("polars")
1 passed in 0.01s

Same Concept, Different Syntax



from unittest.mock import patch

with patch("pointblank.validate._is_lib_present") as mock:
    mock.return_value = False
    validation.get_dataframe_report("polars")
1 passed in 0.01s

Neither

Software Engineering 101

Same Pattern, Every Time

Remember…

Mock what you don’t control, where it enters your code.

When You Shouldn’t Lie

I Have a Confession

I lied to you before.

Same Example, Actually Correct



library(testthat)
test_that("logs dose correctly", {
  local_mocked_bindings(
    Sys.Date = function() as.Date("2026-09-16"),
    .package = "base"
  )
  dose_date <- as.Date("2026-09-16")
  expect_true(was_dose_taken_today(dose_date))
})

Same Example, Actually Correct



library(testthat)
test_that("logs dose correctly", {
  local_mocked_bindings(
    Sys.Date = function() as.Date("2026-09-16")
  )
  dose_date <- as.Date("2026-09-16")
  expect_true(was_dose_taken_today(dose_date))
})
Error in `local_mocked_bindings()`:
Can't find binding for `Sys.Date`.

Same Example, Actually Correct



Sys.Date <- NULL

library(testthat)
test_that("logs dose correctly", {
  local_mocked_bindings(
    Sys.Date = function() as.Date("2026-09-16")
  )
  dose_date <- as.Date("2026-09-16")
  expect_true(was_dose_taken_today(dose_date))
})
Test passed 🎉

Two Three Questions to Ask




What do we need to lie about?
&
Where do we need to lie about it?

&
How do we keep the lie going?

Even the Docs Know

Even the Docs Know

Enter {withr}

Reach for {withr} for environmental control

• Safely and temporarily modify environment

• Manage side effects in your code

• When it’s environmental, start here

Three Ways I’ve Lied Wrong

1. Reaching for mocking, when {withr} was already built for the job


# what I reached for
local_mocked_bindings(
  Sys.getenv = function(x) "fake"
)

# what actually solves it
withr::local_envvar(MY_VAR = "fake")

Three Ways I’ve Lied Wrong

2. Believing a ‘passed’ mock was correct, but not realizing it wasn’t shaped like the real thing


# what the mock returned
list(status = "ok")

# what the real thing returns
list(status = "ok", data = tibble(...), errors = list())

Three Ways I’ve Lied Wrong

3. Mocking so deep into an abstraction and not realizing, it wasn’t that deep


outer_fx <- function() {
  pre_inner_fx() # <- Should've mocked here
  inner_fx()
  inner_inner_fx()
  inner_inner_inner_fx() #<- Tried to mock here
}

The Rule

If you’re sure you need to lie, use the simplest tool to lie about the world your code lives in. Never the thing you’re testing.

So, Why Bother?

Next Time You See Chaos

Flip your Script

Stop asking “how do I make my test hit production?”

Start asking “what part of this should I fake?”

Still Messy. Less Scary.

Still Messy. Less Scary.

Fake It ’Til You Make It