Android Studio 3.0
RxJava-1
单元测试以下代码,这些代码将返回一个结果为true或false的结果,具体取决于findLastMessage
是否返回null。
public Single<Boolean> hasLocalStorage() {
return Single.fromCallable(() ->
chatStorageHelper.findLatestMessage() != null);
}
在我的测试用例中,我有以下内容:
storageHelper
是一个模拟器,它将返回此案例的非空值
@Test
public void testHasLocalStorage_hasLocalStorage_returnTrue() {
when(storageHelper.findLatestMessage()).thenReturn(new DBMessage());
final Single<Boolean> result = Single.just(true);
assertThat(localRepository.hasLocalStorage(), is(result));
}
但是,测试总是失败,因为:
Expected :is <rx.internal.util.ScalarSynchronousSingle@712cfb63>
Actual :<rx.Single@32e54a9d>
当我检查Single.just(true)
返回的内容如下:
public static <T> Single<T> just(final T value) {
return ScalarSynchronousSingle.create(value);
}
调用ScalarSynchronousSingle。我想知道如何更改我的测试用例以便从rx.Single
返回Single.just(true)
?
非常感谢任何建议,
答案 0 :(得分:2)
问题在于您尝试从两个不同的Single
断言引用,一个来自您的测试代码,另一个来自您的生产代码。您需要从构造函数中提供Single
,并在测试中,在创建该类时发送它。
答案 1 :(得分:2)
此行不起作用:
let digitSet = CharacterSet.decimalDigits
let myNumbers = String(myString.unicodeScalars.filter { digitSet.contains($0) })
因为Single是一个值的包装器,可以在Future中生成。事实上,如果#hasLocalStorage的引用返回值与结果相同,则断言。
您需要从hasLocalStorage-Single和result中获取值,并比较其值。
例如,您可以使用#toBlocking()并使用#value()保留被动环境。
if sys.platform == "win32":
driver = webdriver.PhantomJS(executable_path=path.realpath("phantomjs.exe"))
elif sys.platform == "darwin":
driver = webdriver.PhantomJS(executable_path=path.realpath("phantomjsMac"))
else:
driver = webdriver.PhantomJS(executable_path=path.realpath("phantomjsLinux"))