Skip to content

Instantly share code, notes, and snippets.

@kazu69
Created May 22, 2012 16:38
Embed
What would you like to do?
Sinon.js stabのsimple test
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Backbone + sinon simple tests</title>
<script type="text/javascript" src='http://documentcloud.github.com/underscore/underscore-min.js'></script>
<script type="text/javascript" src='http://documentcloud.github.com/backbone/backbone.js'></script>
<script type="text/javascript" src='http://sinonjs.org/releases/sinon-1.3.4.js'></script>
<script type="text/javascript">
var Foo = function() {};
Foo.prototype.bar = function(content){ console.log(content); }
var foo = new Foo();
foo.bar('this is foo') // -> this is foo
var stub = sinon.stub(foo, 'bar');
stub.withArgs('this is foo').returns('this is stab');
var res = foo.bar('this is foo'); // -> 'this is stab'
console.log(res);
stub.restore();
foo.bar('this is foo'); // -> this is foo
var baz = function(content){ console.log(content + ' : ' + content); }
var stub = sinon.stub(foo, 'bar', baz);
stub.withArgs('this is foo');
foo.bar('this is foo'); // -> this is foo : this is foo
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment