我正在为使用 video_player 插件的视频播放器小部件编写一个简单的测试。我无法模拟视频控制器发出的用于通过网络获取视频的网络请求。我的小部件代码如下所示:
late VideoPlayerController _videoController;
@override
void initState() {
_videoController = VideoPlayerController.network(widget.videoUrl);
// rest of the code
}
Run Code Online (Sandbox Code Playgroud)
和测试代码:
VideoPlayerController _videoController =
VideoPlayerController.network(videoUrl);
when(VideoPlayerController.network(videoUrl))
.thenAnswer((_) => _videoController);
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它无法正确地存根网络请求方法。有什么正确嘲笑它的想法吗?我的代码中还有其他几个测试,其中我模拟了发出网络请求的 api 客户端类,但这一个有点不同。我正在使用mockito进行嘲笑。
请帮忙!
unit-testing flutter-test flutter-video-player flutter-mockito
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)