Skip to content

Commit b4bb2a8

Browse files
committed
More tests refactored
1 parent 6f333d8 commit b4bb2a8

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

test/git_test.dart

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import 'package:test/test.dart';
1111

1212
import 'helper.dart';
1313

14-
//class MockGitHub extends MockWithNamedArgs implements GitHub {}
15-
1614
class MockGitHub extends Mock implements GitHub {}
1715

1816
void main() {
@@ -151,18 +149,21 @@ void main() {
151149

152150
test('creates valid JSON body', () {
153151
// given
154-
http.Response res = new http.Response('{}', 200);
155-
github
156-
.when(callsTo('request', anything, anything))
157-
.alwaysReturn(new Future.value(res));
152+
http.Response expectedResponse = new http.Response('{}', 200);
153+
when(github.request(any, any,
154+
body: any, headers: typed(any, named: 'headers')))
155+
.thenReturn(new Future.value(expectedResponse));
158156

159157
// when
160158
git.editReference(repo, 'heads/b', someSha, force: true);
161159

162160
// then
163-
LogEntryNamedArgs entry = github.getLogs().first;
164-
Map body = JSON.decode(entry.namedArgs[#body]);
165-
Map headers = entry.namedArgs[#headers];
161+
var captured = verify(github.request(any, any,
162+
body: captureAny, headers: typed(captureAny, named: 'headers')))
163+
.captured;
164+
165+
var body = JSON.decode(captured[0]);
166+
var headers = captured[1];
166167

167168
expect(body['sha'], equals(someSha));
168169
expect(body['force'], equals(true));
@@ -173,52 +174,44 @@ void main() {
173174
group('deleteReference()', () {
174175
test('constructs correct path', () {
175176
// given
176-
http.Response res = new http.Response('{}', 200);
177-
github
178-
.when(callsTo('request', anything, anything))
179-
.alwaysReturn(new Future.value(res));
177+
http.Response expectedResponse = new http.Response('{}', 200);
178+
when(github.request(any, any))
179+
.thenReturn(new Future.value(expectedResponse));
180180

181181
// when
182182
git.deleteReference(repo, 'heads/b');
183183

184184
// then
185-
github
186-
.getLogs(callsTo('request', 'DELETE', '/repos/o/n/git/refs/heads/b'))
187-
.verify(happenedOnce);
185+
verify(github.request('DELETE', '/repos/o/n/git/refs/heads/b'));
188186
});
189187
});
190188

191189
group('getTag()', () {
192190
test('constructs correct path', () {
193191
git.getTag(repo, someSha);
194192

195-
github
196-
.getLogs(callsTo('getJSON', '/repos/o/n/git/tags/someSHA'))
197-
.verify(happenedOnce);
193+
verify(github.getJSON('/repos/o/n/git/tags/someSHA',
194+
convert: GitTag.fromJSON, statusCode: StatusCodes.OK));
198195
});
199196
});
200197

201198
group('createTag()', () {
199+
var createGitTag = new CreateGitTag('v0.0.1', 'a message', someSha,
200+
'commit', new GitCommitUser('aName', 'aEmail', new DateTime.now()));
201+
202202
test('constructs correct path', () {
203-
git.createTag(
204-
repo,
205-
new CreateGitTag('v0.0.1', 'a message', someSha, 'commit',
206-
new GitCommitUser('aName', 'aEmail', new DateTime.now())));
203+
git.createTag(repo, createGitTag);
207204

208-
github
209-
.getLogs(callsTo('postJSON', '/repos/o/n/git/tags'))
210-
.verify(happenedOnce);
205+
verify(github.postJSON('/repos/o/n/git/tags',
206+
convert: GitTag.fromJSON,
207+
statusCode: StatusCodes.CREATED,
208+
body: createGitTag.toJSON()));
211209
});
212210

213211
test('creates valid JSON body', () {
214-
git.createTag(
215-
repo,
216-
new CreateGitTag('v0.0.1', 'a message', someSha, 'commit',
217-
new GitCommitUser('aName', 'aEmail', new DateTime.now())));
218-
219-
LogEntryNamedArgs entry = github.getLogs().first;
220-
Map body = JSON.decode(entry.namedArgs[#body]);
212+
git.createTag(repo, createGitTag);
221213

214+
var body = captureSentBody(github);
222215
expect(body['tag'], equals('v0.0.1'));
223216
expect(body['message'], equals('a message'));
224217
expect(body['object'], equals(someSha));
@@ -312,3 +305,13 @@ captureSentBody(MockGitHub github) {
312305
var body = JSON.decode(bodyString);
313306
return body;
314307
}
308+
309+
captureSentHeaders(MockGitHub github) {
310+
var headersString = verify(github.postJSON(any,
311+
convert: any, statusCode: any, body: any, headers: typed(captureAny)))
312+
.captured
313+
.single;
314+
315+
var headers = JSON.decode(headersString);
316+
return headers;
317+
}

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