I'm trying to mock sharedPreferences using Mockito in my flutter project. Here is the error log.
package:mockito/src/mock.dart 190:7 Mock._noSuchMethod
package:mockito/src/mock.dart 184:45 Mock.noSuchMethod
test\feature\number_trivia\data\datasource\number_trivia_local_datasource_test.mocks.dart 67:14 MockSharedPreferences.setString
package:clean_arch_tdd/features/number_trivia/data/datasources/number_trivia_local_datasource.dart 31:30 NumberTriviaLocalDataSourceImpl.cacheLastNumberTrivia
test\feature\number_trivia\data\datasource\number_trivia_local_datasource_test.dart 51:18 main.<fn>.<fn>
MissingStubError: 'setString'
No stub was found which matches the arguments of this method call:
setString('CACHED_NUMBER_TRIVIA', '{"text":"Test trivia","number":1}')
Add a stub for this method using Mockito's 'when' API, or generate the mock for MockSharedPreferences with 'returnNullOnMissingStub: true'.
Run Code Online (Sandbox Code Playgroud)
The error refer to this line of code.
local_data_source_test.dart
test('should call sharedPreferences to …Run Code Online (Sandbox Code Playgroud) 我有一个双精度值,它是我需要将其转换为持续时间的小时数。假设我有一个像这样的函数原型
Duration parseDurationFromDouble(double d) {
// parse the double
return parsedDuration;
}
Run Code Online (Sandbox Code Playgroud)
和
2.5由于我的持续时间存储为双精度,那么该函数应该返回,如下所示:
Duration(hours: 2, minutes: 30)
如何在 Dart 中做到这一点?可以直接转换吗?或者我应该通过字符串插值来完成此操作?
提前致谢。