如何在Observable RxJava中测试参数

huangapple go评论66阅读模式
英文:

How I can test arguments in Observable RxJava

问题

我有一个方法:

repository.get(arg1, arg2, arg3)

方法返回 Single<List<SomeObject>>

我需要测试调用了特定参数的 SingleonSubscribe() 方法。

repository.get(arg1, arg2, arg3)
            .test()
            .assertSubscribed()

这个方法忽略了我放在 repository.get() 方法中的参数。

我如何测试订阅是否使用了特定参数进行了调用?

英文:

I have some method:

repository.get(arg1, arg2, arg3)

Method return Single&lt;List&lt;SomeObject&gt;&gt;

I need testing that Single was called onSubscribe() with specific arguments.

repository.get(arg1, arg2, arg3)
            .test()
            .assertSubscribed()

This method ignores arguments which I put in method repository.get().

How I can test that subscribe called with certain arguments?

答案1

得分: 1

使用Schedulers.trampoline(),使订阅在当前线程上发生。然后你可以断言所需的参数。

@Before
fun setup() {
    RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
    RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
}

这将使代码在当前线程上运行。
然后你可以断言参数

repository.get(arg1, arg2, arg3)
    .test()
    .subscribe {
         // 在这里进行断言
     }
英文:

use Schedulers.trampoline() to make the subscription occur on the current thread. then you can assert the required args

@Before
    fun setup() {
        RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
        RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
    }

this makes the code run on the current thread.
then you can assert the args

repository.get(arg1, arg2, arg3)
            .test()
            .subscribe {
                 // do the assertions here
             }

huangapple
  • 本文由 发表于 2020年10月2日 02:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64161344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定