Missing stub error on mockito in flutter. Trying to use setString on mocked SharedPreferences

Issue

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

The error refer to this line of code.

local_data_source_test.dart

test('should call sharedPreferences to cache the data', () {
      dataSource.cacheLastNumberTrivia(tNumberTriviaModel);
      final expectedJsonString = jsonEncode(tNumberTriviaModel.toJson());
      verify(mockSharedPreferences.setString(
          cachedNumberTrivia, expectedJsonString));
    });

local_data_source.dart

@override
  Future<void> cacheLastNumberTrivia(NumberTriviaModel triviaToCache) {
    return sharedPreferences.setString(
        cachedNumberTrivia, jsonEncode(triviaToCache.toJson()));
  }

It show that method setString from the mocked sharedPreferences is missing. I already run the pub command to generate the mocks. I also have some test case in the file that use the getString method. And it works fine.

Is there something I’m missing so I can’t use the setString method? Or are there any solution to this problem?

Thx in advance.

Solution

I actually figured it out, sorry for not posting the answer immediately.

I found myself forgot to call the stub for the setString method. Here is the code.

group('cacheNumberTrivia', () {
    const tNumberTriviaModel =
        NumberTriviaModel(number: 1, text: 'Test trivia');

    test('should call sharedPreferences to cache the data', () async {
      when(mockSharedPreferences.setString(any, any))
          .thenAnswer((_) async => true);
      dataSource.cacheLastNumberTrivia(tNumberTriviaModel);
      final expectedJsonString = jsonEncode(tNumberTriviaModel.toJson());
      verify(mockSharedPreferences.setString(
          cachedNumberTrivia, expectedJsonString));
    });
  });

The stub was actually this line:

when(mockSharedPreferences.setString(any, any))
          .thenAnswer((_) async => true);

I forgot to call the when method before verifying the result with verify, hence it throws an error.

Answered By – Naufal Rajabi

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.