Skip to content

Instantly share code, notes, and snippets.

@afiore
Created April 28, 2011 15:07
Show Gist options
  • Save afiore/946525 to your computer and use it in GitHub Desktop.
Save afiore/946525 to your computer and use it in GitHub Desktop.
Sinon.js spies fail with objects using light-traits.js and cortex.js
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Spying on objects created through Cortex and light-traits.js </title>
<!-- light-traits.js and cortex.js, twaked so that I don't need to use requireJS or similar libraries -->
<script type="text/javascript" src="https://gist.github.com/raw/946407/4bc11f02474bd466d768589fd0ffaf92195f19ff/light-traits.js"></script>
<script type="text/javascript" src="https://gist.github.com/raw/946411/a106ac47587356f6e235136f80692958b324bf8a/cortex.js"></script>
<script type="text/javascript" src="http://sinonjs.org/releases/sinon-1.0.0.js"></script>
</head>
<body>
<h2>Spying on objects created through Cortex and light-traits.js</h2>
<p>
<code>Sinon.spy</code> should be able to spy on a 'public' object method created through <code>light-traits</code>
<div>[<span id="result-1"></span>]</div>
</p>
<code>Sinon.spy</code> should be able to spy on a 'public' object method created through <code>light-traits</code> and wrapped into a <code>cortex</code> call.
<div>[<span id="result-2"></span>]</div>
</p>
<span></span>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
'use strict';
function runTest (selector, useCortex) {
useCortex = useCortex || false;
var instance = FooBar(useCortex);
sinon.spy(instance, 'sayHello');
instance.run();
if (instance.sayHello.called) {
document.querySelector(selector).textContent = "OK";
} else {
document.querySelector(selector).textContent = "FAILED";
}
}
var FooTrait = Trait({
_secret: "my secret"
}),
BarTrait = Trait({
sayHello: function () {
return "hello this is "+this._secret;
}
}),
FooBarTrait = Trait.compose(
FooTrait,
BarTrait,
Trait({
run: function(){
return this.sayHello();
}
})
),
//constructor
FooBar = function (useCortex) {
var instance = FooBarTrait.create(FooBar.prototype);
return useCortex ? cortex(instance) : instance;
};
runTest('#result-1');
runTest('#result-2',true);
}, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment