Я запускаю несколько юнит-тестов для общих настроек.Я делал вспомогательный класс, чтобы нам не приходилось писать один и тот же код снова и снова.
Что бы я ни пытался, я всегда получаю одну и ту же ошибку снова и снова.Вы найдете класс, который я тестирую, тестовый класс и трассировку стека.
Надеюсь, это что-то глупое, что я забыл.Я попробовал эти ресурсы:
shared_preferences_helper_test.dart:
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shoppaworld_client_app/shared_preferences_helper.dart';
// at the moment testing
void main() {
SharedPreferencesHelper sharedPreferencesHelper;
setUpAll(() {
sharedPreferencesHelper = SharedPreferencesHelper();
const MethodChannel('plugins.flutter.io/shared_preferences')
.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getAll') {
return <String, dynamic> {
'test-string' : 'string-value',
'test-int' : 420,
'test-bool' : true
}; // set initial values here if desired
}
return null;
});
});
test('test if the testing works', () async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setString('test-string-infunction', 'wow');
var value = sharedPreferences.getString('test-string-infunction');
print(value);
expect(value, 'wow');
});
group('unit test 1: read shared preferences', () {
test('shared preferences string', () async {
String value = await sharedPreferencesHelper.readValueFromKey("test-string");
print(value);
expect(value, 'string-value');
});
test('shared preferences empty', () async {
var value = await sharedPreferencesHelper.readValueFromKey("does_not_exist");
expect(value, null);
});
});
group('unit test 2: write shared preferences', () {
test('shared preferences string', () async {
var originalValue = 'test';
var value = await sharedPreferencesHelper.writeValueToKey("test-string", valueString: originalValue);
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
expect(sharedPreferences.getString('test-string'), originalValue);
});
test('shared preferences int', () async {
var originalValue = 420;
var value = await sharedPreferencesHelper.writeValueToKey("test-int", valueInt: originalValue);
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
expect(sharedPreferences.getInt('test-int'), originalValue);
});
test('shared preferences bool', () async {
var originalValue = true;
var value = await sharedPreferencesHelper.writeValueToKey("test-bool", valueBool: originalValue);
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
expect(sharedPreferences.getBool('test-bool'), originalValue);
});
});
}
shared_preferences_helper.dart:
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
// this class is meant to relieve developers from boilerplate code
class SharedPreferencesHelper {
Future<dynamic> readValueFromKey(String key) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.get(key) ?? null;
}
writeValueToKey(String key, {String valueString, bool valueBool, int valueInt}) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
if(valueString != null){
sharedPreferences.setString(key, valueString);
}
if(valueBool != null) {
sharedPreferences.setBool(key, valueBool);
}
if(valueInt != null) {
sharedPreferences.setInt(key, valueInt);
}
}
}
stacktrace:
Testing started at 12:10 ...
/home/geordy/SoftwareDevelopment/flutter/flutter_linux_v1.2.1-stable/flutter/bin/flutter --no-color test --machine test/shared_preferences_helper_test.dart
dart:core _AssertionError._throwNew
package:shared_preferences/shared_preferences.dart 33:16 SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _AsyncAwaitCompleter.completeError
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
test/shared_preferences_helper_test.dart 30:67 main.<fn>
'package:shared_preferences/shared_preferences.dart': Failed assertion: line 33 pos 16: 'key.startsWith(_prefix)': is not true.
dart:core _AssertionError._throwNew
package:shared_preferences/shared_preferences.dart 33:16 SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _AsyncAwaitCompleter.completeError
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
package:shoppaworld_client_app/shared_preferences_helper.dart 20:67 SharedPreferencesHelper.readValueFromKey
'package:shared_preferences/shared_preferences.dart': Failed assertion: line 33 pos 16: 'key.startsWith(_prefix)': is not true.
dart:core _AssertionError._throwNew
package:shared_preferences/shared_preferences.dart 33:16 SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _AsyncAwaitCompleter.completeError
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
package:shoppaworld_client_app/shared_preferences_helper.dart 20:67 SharedPreferencesHelper.readValueFromKey
'package:shared_preferences/shared_preferences.dart': Failed assertion: line 33 pos 16: 'key.startsWith(_prefix)': is not true.
dart:core _AssertionError._throwNew
package:shared_preferences/shared_preferences.dart 33:16 SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _AsyncAwaitCompleter.completeError
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
package:shoppaworld_client_app/shared_preferences_helper.dart 25:67 SharedPreferencesHelper.writeValueToKey
'package:shared_preferences/shared_preferences.dart': Failed assertion: line 33 pos 16: 'key.startsWith(_prefix)': is not true.
dart:core _AssertionError._throwNew
package:shared_preferences/shared_preferences.dart 33:16 SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _AsyncAwaitCompleter.completeError
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
package:shoppaworld_client_app/shared_preferences_helper.dart 25:67 SharedPreferencesHelper.writeValueToKey
'package:shared_preferences/shared_preferences.dart': Failed assertion: line 33 pos 16: 'key.startsWith(_prefix)': is not true.
dart:core _AssertionError._throwNew
package:shared_preferences/shared_preferences.dart 33:16 SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _AsyncAwaitCompleter.completeError
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:shared_preferences/shared_preferences.dart SharedPreferences.getInstance
package:shoppaworld_client_app/shared_preferences_helper.dart 25:67 SharedPreferencesHelper.writeValueToKey
'package:shared_preferences/shared_preferences.dart': Failed assertion: line 33 pos 16: 'key.startsWith(_prefix)': is not true.