Skip to content

Commit 974122b

Browse files
authored
Merge pull request SpinlockLabs#84 from DirectMyFile/refactor_tests_to_mockito
Refactor tests to mockito
2 parents 1ab504b + 865eadf commit 974122b

File tree

4 files changed

+93
-167
lines changed

4 files changed

+93
-167
lines changed

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ dependencies:
1313
markdown: '^0.11.1'
1414
dev_dependencies:
1515
browser: '^0.10.0+2'
16-
mock: '^0.12.0'
1716
test: '^0.12.0'
17+
mockito: '^1.0.1'

test/git_test.dart

Lines changed: 92 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,29 @@ import 'dart:convert' show JSON;
66
import 'package:github/src/common.dart';
77
import 'package:github/src/util.dart';
88
import "package:http/http.dart" as http;
9-
import 'package:mock/mock.dart';
9+
import 'package:mockito/mockito.dart';
1010
import 'package:test/test.dart';
1111

12-
import 'helper.dart';
13-
14-
class MockGitHub extends MockWithNamedArgs implements GitHub {}
12+
class MockGitHub extends Mock implements GitHub {}
1513

1614
void main() {
1715
MockGitHub github;
1816
GitService git;
1917
RepositorySlug repo;
18+
var someSha = 'someSHA';
2019

2120
setUp(() {
2221
github = new MockGitHub();
2322
git = new GitService(github);
2423
repo = new RepositorySlug('o', 'n');
2524
});
2625

27-
//
28-
// BLOB
29-
//
3026
group('getBlob()', () {
3127
test('constructs correct path', () {
3228
git.getBlob(repo, 'sh');
33-
github
34-
.getLogs(callsTo('getJSON', '/repos/o/n/git/blobs/sh'))
35-
.verify(happenedOnce);
29+
30+
verify(github.getJSON('/repos/o/n/git/blobs/sh',
31+
convert: GitBlob.fromJSON, statusCode: StatusCodes.OK));
3632
});
3733
});
3834

@@ -41,33 +37,28 @@ void main() {
4137
CreateGitBlob blob = new CreateGitBlob('bbb', 'utf-8');
4238
git.createBlob(repo, blob);
4339

44-
github
45-
.getLogs(callsTo('postJSON', '/repos/o/n/git/blobs'))
46-
.verify(happenedOnce);
40+
verify(github.postJSON('/repos/o/n/git/blobs',
41+
convert: GitBlob.fromJSON,
42+
statusCode: StatusCodes.CREATED,
43+
body: blob.toJSON()));
4744
});
4845

4946
test('creates valid JSON body', () {
5047
CreateGitBlob blob = new CreateGitBlob('bbb', 'utf-8');
51-
git.createBlob(repo, blob);
52-
53-
LogEntryNamedArgs entry = github.getLogs().first;
54-
Map body = JSON.decode(entry.namedArgs[#body]);
5548

49+
git.createBlob(repo, blob);
50+
var body = captureSentBody(github);
5651
expect(body['content'], equals('bbb'));
5752
expect(body['encoding'], equals('utf-8'));
5853
});
5954
});
6055

61-
//
62-
// COMMIT
63-
//
6456
group('getCommit()', () {
6557
test('constructs correct path', () {
6658
git.getCommit(repo, 'sh');
6759

68-
github
69-
.getLogs(callsTo('getJSON', '/repos/o/n/git/commits/sh'))
70-
.verify(happenedOnce);
60+
verify(github.getJSON('/repos/o/n/git/commits/sh',
61+
convert: GitCommit.fromJSON, statusCode: StatusCodes.OK));
7162
});
7263
});
7364

@@ -76,9 +67,10 @@ void main() {
7667
CreateGitCommit commit = new CreateGitCommit('aMessage', 'aTreeSha');
7768
git.createCommit(repo, commit);
7869

79-
github
80-
.getLogs(callsTo('postJSON', '/repos/o/n/git/commits'))
81-
.verify(happenedOnce);
70+
verify(github.postJSON('/repos/o/n/git/commits',
71+
convert: GitCommit.fromJSON,
72+
statusCode: StatusCodes.CREATED,
73+
body: commit.toJSON()));
8274
});
8375

8476
test('creates valid JSON body', () {
@@ -94,9 +86,7 @@ void main() {
9486
git.createCommit(repo, commit);
9587

9688
// then
97-
LogEntryNamedArgs entry = github.getLogs().first;
98-
Map body = JSON.decode(entry.namedArgs[#body]);
99-
89+
var body = captureSentBody(github);
10090
expect(body['message'], equals('aMessage'));
10191
expect(body['tree'], equals('aTreeSha'));
10292
expect(body['parents'], equals(['parentSha1', 'parentSha2']));
@@ -109,73 +99,71 @@ void main() {
10999
});
110100
});
111101

112-
//
113-
// REFERENCE
114-
//
115102
group('getReference()', () {
116103
test('constructs correct path', () {
117104
git.getReference(repo, 'heads/b');
118105

119-
github
120-
.getLogs(callsTo('getJSON', '/repos/o/n/git/refs/heads/b'))
121-
.verify(happenedOnce);
106+
verify(github.getJSON('/repos/o/n/git/refs/heads/b',
107+
convert: GitReference.fromJSON, statusCode: StatusCodes.OK));
122108
});
123109
});
124110

125111
group('createReference()', () {
112+
var someRef = 'refs/heads/b';
126113
test('constructs correct path', () {
127-
git.createReference(repo, 'refs/heads/b', 'someSHA');
114+
git.createReference(repo, someRef, someSha);
128115

129-
github
130-
.getLogs(callsTo('postJSON', '/repos/o/n/git/refs'))
131-
.verify(happenedOnce);
116+
verify(github.postJSON('/repos/o/n/git/refs',
117+
convert: GitReference.fromJSON,
118+
statusCode: StatusCodes.CREATED,
119+
body: JSON.encode({'ref': someRef, 'sha': someSha})));
132120
});
133121

134122
test('creates valid JSON body', () {
135-
git.createReference(repo, 'refs/heads/b', 'someSHA');
136-
137-
// then
138-
LogEntryNamedArgs entry = github.getLogs().first;
139-
Map body = JSON.decode(entry.namedArgs[#body]);
123+
git.createReference(repo, someRef, someSha);
140124

141-
expect(body['ref'], equals('refs/heads/b'));
142-
expect(body['sha'], equals('someSHA'));
125+
var body = captureSentBody(github);
126+
expect(body['ref'], equals(someRef));
127+
expect(body['sha'], equals(someSha));
143128
});
144129
});
145130

146131
group('editReference()', () {
147132
test('constructs correct path', () {
148133
// given
149-
http.Response res = new http.Response('{}', 200);
150-
github
151-
.when(callsTo('request', anything, anything))
152-
.alwaysReturn(new Future.value(res));
134+
http.Response expectedResponse = new http.Response('{}', 200);
135+
136+
when(github.request(any, any,
137+
body: any, headers: typed(any, named: 'headers')))
138+
.thenReturn(new Future.value(expectedResponse));
153139

154140
// when
155-
git.editReference(repo, 'heads/b', 'someSHA');
141+
git.editReference(repo, 'heads/b', someSha);
156142

157143
// then
158-
github
159-
.getLogs(callsTo('request', 'PATCH', '/repos/o/n/git/refs/heads/b'))
160-
.verify(happenedOnce);
144+
verify(github.request('PATCH', '/repos/o/n/git/refs/heads/b',
145+
headers: typed(any, named: 'headers'), body: any));
161146
});
162147

163148
test('creates valid JSON body', () {
164149
// given
165-
http.Response res = new http.Response('{}', 200);
166-
github
167-
.when(callsTo('request', anything, anything))
168-
.alwaysReturn(new Future.value(res));
150+
http.Response expectedResponse = new http.Response('{}', 200);
151+
when(github.request(any, any,
152+
body: any, headers: typed(any, named: 'headers')))
153+
.thenReturn(new Future.value(expectedResponse));
169154

170155
// when
171-
git.editReference(repo, 'heads/b', 'someSHA', force: true);
156+
git.editReference(repo, 'heads/b', someSha, force: true);
172157

173158
// then
174-
LogEntryNamedArgs entry = github.getLogs().first;
175-
Map body = JSON.decode(entry.namedArgs[#body]);
176-
Map headers = entry.namedArgs[#headers];
159+
var captured = verify(github.request(any, any,
160+
body: captureAny, headers: typed(captureAny, named: 'headers')))
161+
.captured;
177162

178-
expect(body['sha'], equals('someSHA'));
163+
var body = JSON.decode(captured[0]);
164+
var headers = captured[1];
165+
166+
expect(body['sha'], equals(someSha));
179167
expect(body['force'], equals(true));
180168
expect(headers['content-length'], equals('30'));
181169
});
@@ -184,96 +172,79 @@ void main() {
184172
group('deleteReference()', () {
185173
test('constructs correct path', () {
186174
// given
187-
http.Response res = new http.Response('{}', 200);
188-
github
189-
.when(callsTo('request', anything, anything))
190-
.alwaysReturn(new Future.value(res));
175+
http.Response expectedResponse = new http.Response('{}', 200);
176+
when(github.request(any, any))
177+
.thenReturn(new Future.value(expectedResponse));
191178

192179
// when
193180
git.deleteReference(repo, 'heads/b');
194181

195182
// then
196-
github
197-
.getLogs(callsTo('request', 'DELETE', '/repos/o/n/git/refs/heads/b'))
198-
.verify(happenedOnce);
183+
verify(github.request('DELETE', '/repos/o/n/git/refs/heads/b'));
199184
});
200185
});
201186

202-
//
203-
// TAG
204-
//
205187
group('getTag()', () {
206188
test('constructs correct path', () {
207-
git.getTag(repo, 'someSHA');
189+
git.getTag(repo, someSha);
208190

209-
github
210-
.getLogs(callsTo('getJSON', '/repos/o/n/git/tags/someSHA'))
211-
.verify(happenedOnce);
191+
verify(github.getJSON('/repos/o/n/git/tags/someSHA',
192+
convert: GitTag.fromJSON, statusCode: StatusCodes.OK));
212193
});
213194
});
214195

215196
group('createTag()', () {
197+
var createGitTag = new CreateGitTag('v0.0.1', 'a message', someSha,
198+
'commit', new GitCommitUser('aName', 'aEmail', new DateTime.now()));
199+
216200
test('constructs correct path', () {
217-
git.createTag(
218-
repo,
219-
new CreateGitTag('v0.0.1', 'a message', 'someSHA', 'commit',
220-
new GitCommitUser('aName', 'aEmail', new DateTime.now())));
221-
222-
github
223-
.getLogs(callsTo('postJSON', '/repos/o/n/git/tags'))
224-
.verify(happenedOnce);
201+
git.createTag(repo, createGitTag);
202+
203+
verify(github.postJSON('/repos/o/n/git/tags',
204+
convert: GitTag.fromJSON,
205+
statusCode: StatusCodes.CREATED,
206+
body: createGitTag.toJSON()));
225207
});
226208

227209
test('creates valid JSON body', () {
228-
git.createTag(
229-
repo,
230-
new CreateGitTag('v0.0.1', 'a message', 'someSHA', 'commit',
231-
new GitCommitUser('aName', 'aEmail', new DateTime.now())));
232-
233-
LogEntryNamedArgs entry = github.getLogs().first;
234-
Map body = JSON.decode(entry.namedArgs[#body]);
210+
git.createTag(repo, createGitTag);
235211

212+
var body = captureSentBody(github);
236213
expect(body['tag'], equals('v0.0.1'));
237214
expect(body['message'], equals('a message'));
238-
expect(body['object'], equals('someSHA'));
215+
expect(body['object'], equals(someSha));
239216
expect(body['type'], equals('commit'));
240217
expect(body['tagger']['name'], equals('aName'));
241218
});
242219
});
243220

244-
//
245-
// TREE
246-
//
247-
//
248-
// TREE
249-
//
250221
group('getTree()', () {
251222
test('constructs correct path', () {
252223
git.getTree(repo, 'sh');
253224

254-
github
255-
.getLogs(callsTo('getJSON', '/repos/o/n/git/trees/sh'))
256-
.verify(happenedOnce);
225+
verify(github.getJSON('/repos/o/n/git/trees/sh',
226+
convert: GitTree.fromJSON, statusCode: StatusCodes.OK));
257227
});
258228
});
259229

260230
group('getTree(recursive: true)', () {
261231
test('constructs correct path', () {
262232
git.getTree(repo, 'sh', recursive: true);
263233

264-
github
265-
.getLogs(callsTo('getJSON', '/repos/o/n/git/trees/sh?recursive=1'))
266-
.verify(happenedOnce);
234+
verify(github.getJSON('/repos/o/n/git/trees/sh?recursive=1',
235+
convert: GitTree.fromJSON, statusCode: StatusCodes.OK));
267236
});
268237
});
269238

270239
group('createTree()', () {
271240
test('constructs correct path', () {
272-
git.createTree(repo, new CreateGitTree([]));
241+
var createGitTree = new CreateGitTree([]);
242+
git.createTree(repo, createGitTree);
273243

274-
github
275-
.getLogs(callsTo('postJSON', '/repos/o/n/git/trees'))
276-
.verify(happenedOnce);
244+
verify(github.postJSON('/repos/o/n/git/trees',
245+
convert: GitTree.fromJSON,
246+
statusCode: StatusCodes.CREATED,
247+
body: createGitTree.toJSON()));
277248
});
278249

279250
test('with sha creates valid JSON body', () {
@@ -287,9 +258,7 @@ void main() {
287258
git.createTree(repo, tree);
288259

289260
// then
290-
LogEntryNamedArgs entry = github.getLogs().first;
291-
Map body = JSON.decode(entry.namedArgs[#body]);
292-
261+
var body = captureSentBody(github);
293262
expect(body['tree'], isNotNull);
294263
expect(body['tree'][0]['path'], equals('file.rb'));
295264
expect(body['tree'][0]['mode'], equals('100644'));
@@ -310,9 +279,7 @@ void main() {
310279
git.createTree(repo, tree);
311280

312281
// then
313-
LogEntryNamedArgs entry = github.getLogs().first;
314-
Map body = JSON.decode(entry.namedArgs[#body]);
315-
282+
var body = captureSentBody(github);
316283
expect(body['tree'], isNotNull);
317284
expect(body['tree'][0]['path'], equals('file.rb'));
318285
expect(body['tree'][0]['mode'], equals('100644'));
@@ -322,3 +289,13 @@ void main() {
322289
});
323290
});
324291
}
292+
293+
Map<String, dynamic> captureSentBody(MockGitHub github) {
294+
var bodyString = verify(
295+
github.postJSON(any, convert: any, statusCode: any, body: captureAny))
296+
.captured
297+
.single;
298+
299+
var body = JSON.decode(bodyString) as Map<String, dynamic>;
300+
return body;
301+
}

test/helper.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import 'dart:io';
66

77
import "package:http/http.dart" as http;
88
import 'package:github/server.dart';
9-
import 'package:mock/mock.dart';
109
import 'package:test/test.dart';
1110

1211
part 'helper/assets.dart';
1312
part 'helper/expect.dart';
1413
part 'helper/http.dart';
15-
part 'helper/mock.dart';
1614

1715
GitHub github = _makeGitHubClient();
1816

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy