mockkのverifyでメソッドの引数の中身をチェックする
下のようなコードでUseCaseSaveのテストでrepo.saveに意図した値が入っているかを確認したいようなケース
data class Data( val target: Int val timestamp: Date = Date() ) class UseCaseSave(val repo: Repo) { fun execute(num: Int) { repo.save(Data(num)) } }
matcherを自前で用意することで対応が可能。
@Test fun test_hoge() { val repo = mockk() val useCase = UseCaseSave(repo) useCase.execute(1) verify { repo.save(match { date -> date.target == 1 }) } }
CircleCIでprivateなリポジトリをgit submoduleでアクセス出来るようにする
Projectのページ開いて右の方にある Project Settings
を開く
左のメニューから SSH Keys
を選ぶ
User Key
で Add User Key
を押す
こんな感じに追加されたらアクセス出来る状態になる
あとはCircleCIのymlでこんな感じのを追加して呼び出せばOK
init_git_submodule: steps: - run: name: Git submodule command: git submodule update --init
SpekでgetMainLooperをMockする必要がある場合の対応方法
LiveData使ってるクラスのテストをSpekで書こうとするとよく出てくるエラー。
エラー内容
Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.
テスト用のTaskExecutorを用意する
class TestArchTaskExecutor : TaskExecutor() { override fun executeOnDiskIO(runnable: Runnable) { runnable.run() } override fun isMainThread(): Boolean { return true } override fun postToMainThread(runnable: Runnable) { runnable.run() } }
SpekのGroupBodyに拡張関数を追加
fun GroupBody.applyTestTaskExecutor(executor: TaskExecutor = TestArchTaskExecutor()) { beforeGroup { // AACのテスト用設定 ArchTaskExecutor.getInstance().setDelegate(executor) } afterGroup { ArchTaskExecutor.getInstance().setDelegate(null) } }
テストでSpekのBodyで拡張関数を呼び出す
internal object ViewModelTest : Spek({ applyTestTaskExecutor() Feature("...") {} }
JUnit5でコルーチンの例外発生のテストを書く
@Test fun test_exception() { assertThrows(IllegalArgumentException::class.java) { runBlocking { testMethod() } } }
assertThrowsにラムダを渡すことで例外発生のテストを書くことが出来る
aarに含まれるsoが見つからない
これが正しい
artifacts.add("default", file('libname.aar'))
こんな感じに拡張子名を忘れるとライブラリが見つからないとエラーになる
artifacts.add("default", file('libname'))
pandasでステップ状のグラフを書く
import pandas as pd df = pd.DataFrame([1, 3 , 5]) df.plot(marker='.') df.plot(marker='x', drawstyle='steps-post') # drawstyleで指定
drawstyleで steps-post
を指定するとステップ状になる
指定なしの場合
指定ありの場合