Skip to content

test(ads-client): assert mockito expectations to prevent silent false-positives#7347

Open
Almaju wants to merge 1 commit intomozilla:mainfrom
Almaju:ac-ads-client-mock-verify
Open

test(ads-client): assert mockito expectations to prevent silent false-positives#7347
Almaju wants to merge 1 commit intomozilla:mainfrom
Almaju:ac-ads-client-mock-verify

Conversation

@Almaju
Copy link
Copy Markdown
Contributor

@Almaju Almaju commented May 4, 2026

Summary

Some ads-client tests configured mockito mocks with .expect(N) to declare an expected request count, but never called .assert() to verify it. Without the assert, mockito does not enforce the expectation, so tests pass even when the underlying behavior is wrong.

This PR pairs every interaction-verifying mock setup with an explicit .assert() call across mars.rs, http_cache.rs, and client.rs (including the ignored test_record_click_invalidates_cache).

While doing this, the missing .assert() surfaced a real silent false-positive: mars::tests::test_fetch_ads_cache_hit_skips_network was constructing the test client with None for the cache, so both calls hit the network and the cache-hit path the test claimed to verify was never actually exercised. The test now constructs a real HttpCache so the second call serves from cache, and the .assert() enforces that only one network request was made.

A couple of duplicate-mock setups in http_cache.rs (test_refresh_policy_always_uses_network_then_caches, test_expired_entry_is_a_miss_on_next_send) were collapsed into a single mock with expect(2). The bodies were never inspected, and a single mock makes the network call count directly verifiable. test_not_cacheable_no_store was tightened to assert both calls hit the network as MissNotCacheable.

Test plan

  • cargo test -p ads-client --lib — all 77 tests pass (1 ignored, unchanged)
  • Confirmed the previously silent test failure is now caught: reverting the make_test_client(Some(cache)) change in test_fetch_ads_cache_hit_skips_network causes the test to fail with "Expected 1 request(s) ... but received 2"

…-positives

Mockito mocks set up with .expect(N) only verify the call count when
.assert() is invoked — without it, tests pass even if the expected
network interactions never happen. Pair every interaction-verifying
mock setup with an explicit .assert().

Also fix test_fetch_ads_cache_hit_skips_network, which passed None for
the cache and so always hit the network twice; the missing .assert()
hid this. The test now uses a real HttpCache so the cache-hit path is
actually exercised.
Copy link
Copy Markdown
Contributor

@copyrighthero copyrighthero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally that the test ran successfully!

@copyrighthero
Copy link
Copy Markdown
Contributor

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants