Skip to content

Commit debeec2

Browse files
committed
Save created models in closure to allow calling given(User|Model) multiple times in the same test
Fix iGLOO-be#1
1 parent c9768f2 commit debeec2

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/helpers.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ _beforeEach.withArgs = function() {
7878

7979
_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
8080
var modelKey = modelName;
81+
var modelInstance;
8182

8283
if(typeof attrs === 'function') {
8384
optionalHandler = attrs;
@@ -113,6 +114,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
113114
done(err);
114115
} else {
115116
test[modelKey] = result;
117+
modelInstance = result;
116118
done();
117119
}
118120
});
@@ -123,7 +125,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
123125
}
124126

125127
afterEach(function(done) {
126-
this[modelKey].destroy(done);
128+
modelInstance.destroy(done);
127129
});
128130
}
129131

@@ -139,6 +141,8 @@ _beforeEach.givenUser = function(attrs, optionalHandler) {
139141
}
140142

141143
_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
144+
var roleInstance, roleMappingInstance;
145+
142146
if (typeof role === 'string') {
143147
role = {
144148
name: role
@@ -154,6 +158,8 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
154158
}
155159

156160
test.userRole = result;
161+
roleInstance = result;
162+
157163
test.app.models.RoleMapping.create(
158164
{principalId: test.__USERMODEL__.id,
159165
principalType: test.app.models.RoleMapping.USER,
@@ -166,6 +172,8 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
166172
}
167173

168174
test.userRoleMapping = result;
175+
roleMappingInstance = result;
176+
169177
done();
170178
}
171179
);
@@ -177,14 +185,13 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
177185
}
178186

179187
afterEach(function(done) {
180-
var test = this;
181-
this.userRole.destroy(function(err) {
188+
roleInstance.destroy(function(err) {
182189
if(err) return done(err);
183-
test.userRole = undefined;
190+
roleInstance = undefined;
184191

185-
test.userRoleMapping.destroy(function(err) {
192+
roleMappingInstance.destroy(function(err) {
186193
if(err) return done(err);
187-
test.userRoleMapping = undefined;
194+
roleMappingInstance = undefined;
188195
done();
189196
});
190197
});

0 commit comments

Comments
 (0)