Skip to content

Commit

Permalink
feat: Add dependency injection to make testing easier
Browse files Browse the repository at this point in the history
  • Loading branch information
tohhongxiang committed Nov 1, 2021
1 parent 99563bf commit ba70715
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/models/get_song_bpm_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ class GetSongBPMModel {
'Accept': 'application/json',
};

// dependency injection of http client
late http.Client client;

This comment has been minimized.

Copy link
@darylhjd

darylhjd Nov 1, 2021

Member

does the client need to be created outside the model or can the model create its own client

GetSongBPMModel(http.Client client) {
this.client = client;
}

// getSongs : Returns a list of songs with a given BPM.
static Future<List<TempoSong>> getSongs(int bpm) async {
Future<List<TempoSong>> getSongs(int bpm) async {
log("Getting songs of BPM $bpm");
final queryParams = {
"api_key": Config.getSongBpmApiKey,
Expand Down
7 changes: 5 additions & 2 deletions lib/models/spotify_controller_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:spotify_sdk/models/connection_status.dart';
import 'package:spotify_sdk/models/image_uri.dart';
import 'package:spotify_sdk/models/player_state.dart';
import 'package:spotify_sdk/spotify_sdk.dart';
import 'package:http/http.dart' as http;

// SpotifyControllerModel is in charge of maintaining connection between
// the app and the Spotify app, as well as for managing the player.
Expand Down Expand Up @@ -59,7 +60,9 @@ class SpotifyControllerModel with ChangeNotifier {
String _cadenceValue = "-";
String _cadenceStatus = "Inactive";

SpotifyControllerModel(this._ctx) {
// http client for fetching (dependency injection)
http.Client client;
SpotifyControllerModel(this._ctx, this.client) {
// Set inactive state.
_setInactiveState();
// Initialise required connections to Spotify app.
Expand Down Expand Up @@ -211,7 +214,7 @@ class SpotifyControllerModel with ChangeNotifier {
TempoSong selectedSong;
final String uri;
try {
songs = await GetSongBPMModel.getSongs(cadence);
songs = await GetSongBPMModel(this.client).getSongs(cadence);
if (!_isActive) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/screens/main_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:provider/provider.dart';
import 'package:spotify_sdk/models/image_uri.dart';
import 'package:spotify_sdk/models/player_state.dart';
import 'package:tuple/tuple.dart';
import 'package:http/http.dart' as http;

// MainScreen is the screen that the user will see after confirming connection
// with Spotify.
Expand Down Expand Up @@ -47,7 +48,7 @@ class _MainScreenBodyState extends State<_MainScreenBody> {
@override
void initState() {
super.initState();
_spotifyModel = SpotifyControllerModel(context);
_spotifyModel = SpotifyControllerModel(context, http.Client());
}

@override
Expand Down
17 changes: 17 additions & 0 deletions test/spotify_controller_model_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:catch_my_cadence/models/spotify_controller_model.dart';
import 'package:catch_my_cadence/screens/main_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/testing.dart';

void main() async {
setUp(() async {
await dotenv.load(
fileName: "assets/secrets.env"); // MainScreen requires secrets
});

group("Main Screen", () {
test('', () async {});
});
}

0 comments on commit ba70715

Please sign in to comment.