Coder Social home page Coder Social logo

jachenry / livedata-testing Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jraska/livedata-testing

0.0 0.0 0.0 142 KB

TestObserver to easily test LiveData and make assertions on them.

License: Apache License 2.0

Kotlin 46.79% Java 53.21%

livedata-testing's Introduction

LiveData Testing

TestObserver to easily test LiveData and make assertions on them.

CircleCI Download License Android Arsenal

Read Medium Article for more info.

Explanatory Diagram

Usage

Having LiveData<Integer> of counter from 0 to 4:

Kotlin - see ExampleTest.kt

liveData.test()
  .awaitValue()
  .assertHasValue()
  .assertValue { it > 3 }
  .assertValue(4)
  .assertHistorySize(5)
  .assertNever { it > 4 }


// Assertion on structures with a lot of nesting
viewLiveData.map { it.items[0].header.title }
  .assertValue("Expected title")

Java - see ExampleTest.java

TestObserver.test(liveData)
  .awaitValue()
  .assertHasValue()
  .assertValue(value -> value > 3)
  .assertValue(4)
  .assertHistorySize(5)
  .assertNever(value -> value > 4);

Don't forget to use InstantTaskExecutorRule from androidx.arch.core:core-testing to make your LiveData test run properly.

Download

Kotlin users:
testImplementation 'com.jraska.livedata:testing-ktx:1.0.0'
Java users:
testImplementation 'com.jraska.livedata:testing:1.0.0'

If you are not using androidx namespace yet, please use version 0.2.1. Explanation here.

Philosophy

This library is created in a belief that to effective and valuable test should be fast to write and model real code usage. As by Architecture components spec Activity should communicate with its ViewModel only through observing LiveData. TestObserver in this case simulates the Activity and by testing LiveData, we could test our whole logic except the View where the responsibility belongs to Activity. Key ideas:

  • Test pretends to be an Activity
  • No Android framework mocking or Robolectric - just standard fast JUnit tests
  • Fluent API inspired by RxJava TestObserver
  • Easy to write fast executing tests - possibly TDD

livedata-testing's People

Contributors

jraska avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.