Skip to content

Commit

Permalink
Add a shared preferences test and explain how to test (flutter#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
collinjackson authored Jun 5, 2017
1 parent b7040ef commit 4b84d5a
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [0.2.3+1] - 2017-06-04

* Added setMockInitialValues
* Added a test
* Updated README

## [0.2.3] - 2017-06-02

* Suppress warning about unchecked operations when compiling for Android
Expand Down
13 changes: 13 additions & 0 deletions packages/shared_preferences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ _incrementCounter() async {
}
```

### Testing

You can populate `SharedPreferences` with initial values in your tests by running this code:

```
const MethodChannel('plugins.flutter.io/shared_preferences')
.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getAll') {
return <String, dynamic>{}; // set initial values here if desired
}
return null;
});
```
12 changes: 12 additions & 0 deletions packages/shared_preferences/lib/shared_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

const MethodChannel _kChannel =
const MethodChannel('plugins.flutter.io/shared_preferences');
Expand Down Expand Up @@ -103,4 +104,15 @@ class SharedPreferences {
_preferenceCache.clear();
return await _kChannel.invokeMethod('clear');
}

/// Initializes the shared preferences with mock values for testing.
@visibleForTesting
static void setMockInitialValues(Map<String, dynamic> values) {
_kChannel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getAll') {
return values;
}
return null;
});
}
}
7 changes: 6 additions & 1 deletion packages/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: shared_preferences

version: 0.2.3
version: 0.2.4
description: A Flutter plugin for reading and writing simple key-value pairs.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences
Expand All @@ -11,8 +11,13 @@ flutter:
pluginClass: SharedPreferencesPlugin

dependencies:
meta: ^1.0.4
flutter:
sdk: flutter

dev_dependencies:
mockito: ^2.0.2
test: ^0.12.20

environment:
sdk: ">=1.8.0 <2.0.0"
115 changes: 115 additions & 0 deletions packages/shared_preferences/test/shared_preferences_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

import 'package:flutter/services.dart';

import 'package:shared_preferences/shared_preferences.dart';

void main() {
group('$SharedPreferences', ()
{
const MethodChannel channel = const MethodChannel(
'plugins.flutter.io/shared_preferences',
);

const kTestValues = const <String, dynamic>{
'flutter.String': 'hello world',
'flutter.bool': true,
'flutter.int': 42,
'flutter.double': 3.14159,
'flutter.List': const <String>['foo', 'bar'],
};

const kTestValues2 = const <String, dynamic>{
'flutter.String': 'goodbye world',
'flutter.bool': false,
'flutter.int': 1337,
'flutter.double': 2.71828,
'flutter.List': const <String>['baz', 'quox'],
};

final List<MethodCall> log = <MethodCall>[];
SharedPreferences sharedPreferences;

setUp(() async {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
log.add(methodCall);
if (methodCall.method == 'getAll') {
return kTestValues;
}
return null;
});
sharedPreferences = await SharedPreferences.getInstance();
log.clear();
});

test('reading', () async {
expect(sharedPreferences.getString('String'), kTestValues['flutter.String']);
expect(sharedPreferences.getBool('bool'), kTestValues['flutter.bool']);
expect(sharedPreferences.getInt('int'), kTestValues['flutter.int']);
expect(sharedPreferences.getDouble('double'), kTestValues['flutter.double']);
expect(sharedPreferences.getStringList('List'), kTestValues['flutter.List']);
expect(log, equals([]));
});

test('writing', () async {
sharedPreferences.setString('String', kTestValues2['flutter.String']);
sharedPreferences.setBool('bool', kTestValues2['flutter.bool']);
sharedPreferences.setInt('int', kTestValues2['flutter.int']);
sharedPreferences.setDouble('double', kTestValues2['flutter.double']);
sharedPreferences.setStringList('List', kTestValues2['flutter.List']);
expect(sharedPreferences.getString('String'), kTestValues2['flutter.String']);
expect(sharedPreferences.getBool('bool'), kTestValues2['flutter.bool']);
expect(sharedPreferences.getInt('int'), kTestValues2['flutter.int']);
expect(sharedPreferences.getDouble('double'), kTestValues2['flutter.double']);
expect(sharedPreferences.getStringList('List'), kTestValues2['flutter.List']);
expect(log, equals([]));
await sharedPreferences.commit();
expect(log, equals(<MethodCall>[
new MethodCall(
'setString',
{ 'key': 'flutter.String', 'value': kTestValues2['flutter.String'] }
),
new MethodCall(
'setBool',
{ 'key': 'flutter.bool', 'value': kTestValues2['flutter.bool'] }
),
new MethodCall(
'setInt',
{ 'key': 'flutter.int', 'value': kTestValues2['flutter.int'] }
),
new MethodCall(
'setDouble',
{ 'key': 'flutter.double', 'value': kTestValues2['flutter.double'] }
),
new MethodCall(
'setStringList',
{ 'key': 'flutter.List', 'value': kTestValues2['flutter.List'] }
),
new MethodCall('commit'),
]));
});

test('clearing', () async {
await sharedPreferences.clear();
expect(sharedPreferences.getString('String'), null);
expect(sharedPreferences.getBool('bool'), null);
expect(sharedPreferences.getInt('int'), null);
expect(sharedPreferences.getDouble('double'), null);
expect(sharedPreferences.getStringList('List'), null);
expect(log, equals(<MethodCall>[new MethodCall('clear')]));
});

test('mocking', () async {
expect(await channel.invokeMethod('getAll'), kTestValues);
SharedPreferences.setMockInitialValues(kTestValues2);
expect(await channel.invokeMethod('getAll'), kTestValues2);
});
});
}

class MockPlatformChannel extends Mock implements MethodChannel { }

0 comments on commit 4b84d5a

Please sign in to comment.