Created
May 27, 2012 08:46
-
-
Save btakita/2802890 to your computer and use it in GitHub Desktop.
Monkey-patch to add prototypes in describes to allow helper method overriding and mixins in specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyMixin = { | |
bar: function() { | |
return "MyMixin bar"; | |
} | |
}; | |
describe("First Level", function() { | |
var variableFn = function() { | |
return "First Level variableFn"; | |
}; | |
this.prototype.foo = function() { | |
return "First Level foo"; | |
}; | |
jasmine.util.extend(this.prototype, MyMixin); | |
beforeEach(function() { | |
this.variable = variableFn(); | |
this.fooValue = this.foo(); | |
this.barValue = this.bar(); | |
}); | |
it("uses First Level variableFn", function() { | |
expect(this.variable).toEqual("First Level variableFn"); // Fails! | |
// The value is "Second Level variableFn"; | |
}); | |
it("uses First Level foo", function() { | |
expect(this.fooValue).toEqual("First Level foo"); | |
}); | |
it("uses MyMixin bar", function() { | |
expect(this.barValue).toEqual("MyMixin bar"); | |
}); | |
describe("Second Level", function() { | |
variableFn = function() { | |
return "Second Level variableFn"; | |
}; | |
this.prototype.foo = function() { | |
return "Second Level foo"; | |
}; | |
it("uses Second Level variableFn", function() { | |
expect(this.variable).toEqual("Second Level variableFn"); | |
}); | |
it("uses Second Level foo", function() { | |
expect(this.fooValue).toEqual("Second Level foo"); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var oldSuite = jasmine.Suite; | |
jasmine.Suite = function() { | |
oldSuite.apply(this, arguments); | |
this.prototype = {}; | |
}; | |
jasmine.Suite.prototype = oldSuite.prototype; | |
var addAttributes = function() { | |
var specReservedAttributes = {}; | |
(function(specReservedAttributes) { | |
for (var i in this) { | |
if (this.hasOwnProperty(i)) { | |
specReservedAttributes[i] = true; | |
} | |
} | |
}).call(this, specReservedAttributes); | |
var heirarchy = []; | |
(function(heirarchy) { | |
var currentSuite = this.suite; | |
while (currentSuite) { | |
heirarchy.unshift(currentSuite); | |
currentSuite = currentSuite.parentSuite; | |
} | |
}).call(this, heirarchy); | |
var prototypes = {}; | |
(function(prototypes) { | |
for (var i=0; i < heirarchy.length; i++) { | |
jasmine.util.extend(prototypes, heirarchy[i].prototype); | |
} | |
for (var i in prototypes) { | |
if (prototypes.hasOwnProperty(i)) { | |
if (specReservedAttributes[i]) { | |
throw "Cannot override attribute " + i + " because it is used by jasmine"; | |
} | |
} | |
} | |
}).call(this, prototypes); | |
jasmine.util.extend(this, prototypes); | |
}; | |
var addBeforesAndAftersToQueue = jasmine.Spec.prototype.addBeforesAndAftersToQueue; | |
jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { | |
addBeforesAndAftersToQueue.apply(this, arguments); | |
this.queue.addBefore(new jasmine.Block(this.env, addAttributes, this)); | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("PurchaseFlow", function() { | |
var event, purchaseFlow; | |
this.prototype.newEvent = function() { | |
return new Event({}); | |
}; | |
beforeEach(function() { | |
event = this.newEvent(); | |
purchaseFlow = new PurchaseFlow({event: event}); | |
}); | |
it("shows the `#get_ticket` button", function() { | |
}); | |
describe("`#get_ticket`.click", function() { | |
describe("when the event is free", function() { | |
this.prototype.newEvent = function() { | |
return new Event({price_cents: 0}); | |
}; | |
it("creates a Ticket", function() { | |
}); | |
}); | |
describe("when the event is not free", function() { | |
this.prototype.newEvent = function() { | |
return new Event({price_cents: 1000}); | |
}; | |
it("shows the payment options", function() { | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment