agentleFS
StripeKnown publisherstripe/stripe-android/.agents/skills/write-unit-tests/SKILL.md

write-unit-tests

Skill1.5k starsChanged 2 months agoMITView on GitHub

Use when writing or structuring unit tests or fake test implementations in stripe-android — covers scenarios, Turbine interaction tracking and Flow testing, and Truth assertions

6 other projects use a version of this.

Trending

Quiet this year.thin dataabout the whole repository

Jan 2026: 0 mentionsFeb 2026: 0 mentionsMar 2026: 0 mentionsApr 2026: 0 mentionsMay 2026: 0 mentionsJun 2026: 0 mentionsJul 2026: 0 mentionsAug 2026: 0 mentionsSep 2026: 0 mentionsOct 2026: 0 mentionsNov 2026: 0 mentionsDec 2026: 0 mentions
Jan 2026Jul 2026Dec 2026
Mentions a month of the stripe/stripe-android repository that our weekly crawl found: only 6 in all, so this is a hint, not a trend. There is no adoption data for this file yet.

Most recently taken up by

Who's talking about stripe/stripe-android

The biggest 5 of 6 mentions linking to the repository that our crawl found. Each line is their own headline.

See all 6

Talked about on GitHub (6 times).

On GitHub · 6

1 more on GitHub

The file

Show its rules and text

We split this skill into the rules it gives an agent

16 rules. 0 are common, 4 are distinctive, and 12 are very specific to this project.

  • 0

    Common

    Most of the 6 edited versions we read keep these.

  • 4

    Distinctive

    This author's own choices. Worth a look.

  • 12

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use fakes, scenario helpers, and behavior-focused assertions to keep tests reliable and readable.

    line 8
  2. Very specific:

    Use fakes for production collaborators.

    line 12
  3. Distinctive:

    If a production collaborator cannot be faked cleanly, stop and propose a testability seam before writing the test.

    line 13
  4. Very specific:

    Put shared setup in a runScenario helper and validate every fake after the test block.

    line 15
  5. Distinctive:

    Use Google Truth assertions and prefer field-level assertions when exact object equality is not the contract.

    line 16
  6. Very specific:

    Test Flow emissions with Turbine's .test { } API.

    line 18
  7. Very specific:

    All interaction history—including calls, callbacks, and parameterless lifecycle events—must use Turbine.

    line 24
  8. Very specific:

    Use data classes for calls with parameters and Turbine<Unit> for parameterless calls.

    line 58
  9. Very specific:

    Create runScenario and Scenario at the bottom of the test file.

    line 80
  10. Very specific:

    Use runTest directly when shared scenario setup would not help.

    line 121
  11. Very specific:

    Use assertThat(actual).isEqualTo(expected) from Google Truth.

    line 125
  12. Very specific:

    Use Turbine's .test { } API for Flow and StateFlow emissions:

    line 131
  13. Very specific:

    Use runTest, async, and await() so failures propagate.

    line 150
  14. Very specific:

    Use testScheduler.advanceUntilIdle() to advance to a suspension point.

    line 151
  15. Very specific:

    After releasing a latch, use await() because OkHttp resumes asynchronously.

    line 152
  16. Distinctive:

    Use Turbine for StateFlow emission sequences and latches for request ordering.

    line 154

“Common” is measured: kept by at least 6 in 10 of the 6 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Writing Unit Tests

Use fakes, scenario helpers, and behavior-focused assertions to keep tests reliable and readable.

Core Principles

  1. Use fakes for production collaborators. Name them FakeClassName, keep them internal, and place them in the test source set.
  2. If a production collaborator cannot be faked cleanly, stop and propose a testability seam before writing the test.
  3. Track every observed fake interaction with Turbine. Mutable properties are only for configuring behavior.
  4. Put shared setup in a runScenario helper and validate every fake after the test block.
  5. Use Google Truth assertions and prefer field-level assertions when exact object equality is not the contract.
  6. Cover one scenario or configuration per @Test.
  7. Test Flow emissions with Turbine's .test { } API.

Creating Fakes

Give fake constructors sensible defaults so tests only specify behavior relevant to the case. Use a companion create() factory when initialization is complex.

All interaction history—including calls, callbacks, and parameterless lifecycle events—must use Turbine. Do not use mutable lists, counters, wasCalled flags, or callback shortcuts. Retrieve a registered callback from its recorded call so the test also proves registration occurred.

internal class FakeLauncher(
    var launchResult: Boolean = false,
) : Launcher {
    val registerCalls = Turbine<RegisterCall>()
    val launchCalls = Turbine<LaunchCall>()
    val unregisterCalls = Turbine<Unit>()

    override fun register(callback: () -> Unit) {
        registerCalls.add(RegisterCall(callback))
    }

    override fun launch(id: String): Boolean {
        launchCalls.add(LaunchCall(id))
        return launchResult
    }

    override fun unregister() {
        unregisterCalls.add(Unit)
    }

    fun ensureAllEventsConsumed() {
        registerCalls.ensureAllEventsConsumed()
        launchCalls.ensureAllEventsConsumed()
        unregisterCalls.ensureAllEventsConsumed()
    }

    data class RegisterCall(val callback: () -> Unit)
    data class LaunchCall(val id: String)
}

Use data classes for calls with parameters and Turbine<Unit> for parameterless calls. Consume expected calls with awaitItem(), use expectNoEvents() when no call is expected, and include every owned Turbine in ensureAllEventsConsumed().

For view actions, use ViewActionRecorder<ViewAction> instead of another recording mechanism. Accept it as a constructor or factory parameter with a default when useful:

internal class FakeInteractor(
    private val viewActionRecorder: ViewActionRecorder<ViewAction> = ViewActionRecorder(),
) : Interactor {
    override fun handleViewAction(viewAction: ViewAction) {
        viewActionRecorder.record(viewAction)
    }
}

Good codebase references are:

  • paymentsheet/src/test/java/com/stripe/android/paymentsheet/analytics/FakeEventReporter.kt
  • paymentsheet/src/test/java/com/stripe/android/utils/FakeCustomerRepository.kt
  • paymentsheet/src/test/java/com/stripe/android/paymentsheet/verticalmode/FakePaymentMethodVerticalLayoutInteractor.kt

Scenario Pattern

Create runScenario and Scenario at the bottom of the test file. Give setup parameters defaults, expose the system under test and fakes through Scenario, and call each fake's ensureAllEventsConsumed() after the block.

class MyFeatureTest {
    @Test
    fun `fetching data returns success`() = runScenario {
        fakeRepository.result = Result.success(testData)

        val result = systemUnderTest.fetchData()

        assertThat(result.isSuccess).isTrue()
        assertThat(fakeRepository.fetchCalls.awaitItem()).isEqualTo(
            FetchCall(userId = "123")
        )
    }

    private fun runScenario(
        config: Config = defaultConfig,
        block: suspend Scenario.() -> Unit,
    ) = runTest {
        val fakeRepository = FakeRepository()
        val systemUnderTest = MyFeature(
            repository = fakeRepository,
            config = config,
        )

        Scenario(
            systemUnderTest = systemUnderTest,
            fakeRepository = fakeRepository,
        ).apply { block() }

        fakeRepository.ensureAllEventsConsumed()
    }

    private data class Scenario(
        val systemUnderTest: MyFeature,
        val fakeRepository: FakeRepository,
    )
}

Use runTest directly when shared scenario setup would not help. In that case, validate fakes explicitly.

Assertions and Test Scope

Use assertThat(actual).isEqualTo(expected) from Google Truth. Prefer assertions on meaningful fields because their failures are easier to diagnose; use whole-object equality when exact equality is the behavior under test.

Keep each test focused on one case. Split related configurations into separate tests so one failure does not hide another. Test observable behavior rather than implementation details, and include relevant null, empty, error, and boundary cases. Avoid vacuous assertions by establishing the precondition before testing its removal.

Testing Flows

Use Turbine's .test { } API for Flow and StateFlow emissions:

systemUnderTest.state.test {
    assertThat(awaitItem()).isEqualTo(State.Loading)

    fakeRepository.emit(newData)

    assertThat(awaitItem()).isEqualTo(State.Loaded(newData))
    ensureAllEventsConsumed()
}

Common operations are awaitItem(), expectNoEvents(), skipItems(n), and ensureAllEventsConsumed().

Concurrency with Real I/O

For coroutine tests involving real NetworkRule or OkHttp I/O:

  • Use runTest, async, and await() so failures propagate.
  • Use testScheduler.advanceUntilIdle() to advance to a suspension point; never use Thread.sleep.
  • After releasing a latch, use await() because OkHttp resumes asynchronously.
  • Give every CountDownLatch.await() in a network response handler a timeout.
  • Use Turbine for StateFlow emission sequences and latches for request ordering.
  • Assert both that a queued request did not arrive while held and that it arrived after release.

Related Skills

  • Invoke parameterized-tests for parameterized cases.
  • Invoke compose-tests for Compose UI tests.
  • Invoke network-tests for NetworkRule integration tests.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.