如何在Observable RxJava中测试参数

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

How I can test arguments in Observable RxJava

问题

我有一个方法:

  1. repository.get(arg1, arg2, arg3)

方法返回 Single<List<SomeObject>>

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

  1. repository.get(arg1, arg2, arg3)
  2. .test()
  3. .assertSubscribed()

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

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

英文:

I have some method:

  1. 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.

  1. repository.get(arg1, arg2, arg3)
  2. .test()
  3. .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(),使订阅在当前线程上发生。然后你可以断言所需的参数。

  1. @Before
  2. fun setup() {
  3. RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
  4. RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
  5. }

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

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

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

  1. @Before
  2. fun setup() {
  3. RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
  4. RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
  5. }

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

  1. repository.get(arg1, arg2, arg3)
  2. .test()
  3. .subscribe {
  4. // do the assertions here
  5. }

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:

确定