Skip to content

Mock Printer

Eli Hart edited this page Dec 18, 2019 · 5 revisions

Available from 2.0.0-alpha2 release

The mock printer generates Kotlin code that represents the State values for each of your view models.

Usage

First, enable MvRx's mocking system from your Application:

MvRxMocks.install(applicationContext)

Then open up your debug device to the app screen whose State you'd like to copy, connect your device to your computer via adb, and then run the MvRxMockPrinter executable from the command line. =

./MvRxMockPrinter

For State files to be copied into your modules correctly the script expects to be run from your app's root directory.

For more details on this tool, see the README

Arguments

Pass the -h flag to get help and see all available arguments to the script ./MvRxMockPrinter -h

These arguments allow you to control where the generated state is copied to, what view models or fragments it targets, and how lists and strings are handled.

Here is the output at the time of writing.

 ./mock_generation/MvRxMockPrinter -h
Usage: MvRx Mock Printer [-h] [--copyToModule=<copyToModule>]
                         [--excludeRegexes=<excludeRegexes>]
                         [--includeRegexes=<includeRegexes>]
                         [--listTruncationThreshold=<listTruncationThreshold>]
                         [--stringTruncationThreshold=<stringTruncationThreshold
                         >]
Generate MvRx mocks. Creates a mock file for each active ViewModel and MvRxView
in a connected debug app. The device must have debugging enabled. Mocks are
both copied to clipboard and written to temp files. This is intended to be run
from the root directory of your project.
      --copyToModule=<copyToModule>
               Pass the module name of the expected MvRxView so that the generated
                 mock files are copied to the same module automatically. The mocks
                 will be copied to the same package as the MvRxView, but nested
                 under a 'mocks' sub package.
      --excludeRegexes=<excludeRegexes>
               A comma separated list of regexes. Any mockable objects (ie views or
                 view models) whose FQN match one of the regexes will be excluded
                 from state printing have their state printed. A match here trumps a
                 match in includeRegexes.
      --includeRegexes=<includeRegexes>
               A comma separated list of regexes. Any mockable objects (ie views or
                 view models) whose FQN match one of the regexes will have their
                 state printed. If no regexes are provided then all STARTED objects
                 will be included.
      --listTruncationThreshold=<listTruncationThreshold>
               Any lists in the state will be truncated to this many items. This
                 helps to reduce bloat and overhead from lists with many redundant
                 items. Pass 0 to not truncate lists.
                 Default: 3
      --stringTruncationThreshold=<stringTruncationThreshold>
               Any Strings in the state will be truncated to this many characters.
                 This helps to reduce bloat and overhead from very long text. Pass 0
                 to not truncate Strings.
                 Default: 300
  -h, --help   Show help

Which data types are supported

The mock printer reflectively analyzes the properties and values of your States. The script expects these data classes to conform to standard patterns, so it may not work with all classes. Kotlin data classes work best.

If you would like to add support for custom types, you can register support for them via MvRxMocks.mockPrinterConfiguration