-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_version.js
71 lines (60 loc) · 2.39 KB
/
first_version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
init your angular+jasmine tests
define variables "service", "httpBackend" and "fakeApiUrl" in describe block
set them in beforeEach
voila! you can use this thing to test your service which behaves as proxy to $http
*/
"use strict";
var httpBackend, service, fakeApiUrl;
var httpProxy = {
TestCase: function() {
var testCase = this;
testCase.method = "";
testCase.args = [];
testCase.request = {};
testCase.response = {};
this.callMethod = function(methodName) {
testCase.method = methodName;
return testCase;
};
this.withArguments = function() {
testCase.args = arguments;
return testCase;
};
this.expectRequest = function(method, url) {
testCase.request = {
method: method,
url: url
};
return testCase;
};
this.expectResponse = function(code, data) {
testCase.response = {
code: code,
data: data
};
return testCase;
};
},
runTest: function(testCase) {
it('. ' + testCase.method + ' should request correct url', function(done) {
httpBackend["expect"+testCase.request.method]
(fakeApiUrl + testCase.request.url)
.respond(testCase.response.code, testCase.response.data);
var errorCallback = jasmine.createSpy('errorCallback');
var successCallback = function (response) { //jshint ignore:line
expect(response.data).toEqual(testCase.response.data);
expect(response.status).toEqual(testCase.response.code);
done();
};
var expectError = !!testCase.response.data && !!testCase.response.data.errors;
service[testCase.method].apply(service, testCase.args)
.then(
expectError ? errorCallback : successCallback,
expectError ? successCallback : errorCallback
);
httpBackend.flush();
expect(errorCallback).not.toHaveBeenCalled();
});
}
};