forked from 1602/compound
/
crud_controller_test.js
120 lines (106 loc) · 3.29 KB
/
crud_controller_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require('../test_helper.js').controller('models', module.exports);
var sinon = require('sinon');
function ValidAttributes () {
return {
VALID_ATTRIBUTES
};
}
exports['models controller'] = {
'GET new': function (test) {
test.get('/models/new', function () {
test.success();
test.render('new');
test.render('form.' + app.set('view engine'));
test.done();
});
},
'GET index': function (test) {
test.get('/models', function () {
test.success();
test.render('index');
test.done();
});
},
'GET edit': function (test) {
var find = Model.find;
Model.find = sinon.spy(function (id, callback) {
callback(null, new Model);
});
test.get('/models/42/edit', function () {
test.ok(Model.find.calledWith('42'));
Model.find = find;
test.success();
test.render('edit');
test.done();
});
},
'GET show': function (test) {
var find = Model.find;
Model.find = sinon.spy(function (id, callback) {
callback(null, new Model);
});
test.get('/models/42', function (req, res) {
test.ok(Model.find.calledWith('42'));
Model.find = find;
test.success();
test.render('show');
test.done();
});
},
'POST create': function (test) {
var model = new ValidAttributes;
var create = Model.create;
Model.create = sinon.spy(function (data, callback) {
test.strictEqual(data, model);
callback(null, model);
});
test.post('/models', {Model: model}, function () {
test.redirect('/models');
test.flash('info');
test.done();
});
},
'POST create fail': function (test) {
var model = new ValidAttributes;
var create = Model.create;
Model.create = sinon.spy(function (data, callback) {
test.strictEqual(data, model);
callback(new Error, model);
});
test.post('/models', {Model: model}, function () {
test.success();
test.render('new');
test.flash('error');
test.done();
});
},
'PUT update': function (test) {
Model.find = sinon.spy(function (id, callback) {
test.equal(id, 1);
callback(null, {id: 1, updateAttributes: function (data, cb) { cb(null); }});
});
test.put('/models/1', new ValidAttributes, function () {
test.redirect('/models/1');
test.flash('info');
test.done();
});
},
'PUT update fail': function (test) {
Model.find = sinon.spy(function (id, callback) {
test.equal(id, 1);
callback(null, {id: 1, updateAttributes: function (data, cb) { cb(new Error); }});
});
test.put('/models/1', new ValidAttributes, function () {
test.success();
test.render('edit');
test.flash('error');
test.done();
});
},
'DELETE destroy': function (test) {
test.done();
},
'DELETE destroy fail': function (test) {
test.done();
}
};