Last active
December 16, 2015 09:19
-
-
Save hokaccha/5412476 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
mocha.sinon = { | |
test: function(fn) { | |
var sandbox = sinon.sandbox.create(); | |
if (fn.length >= 2) { | |
return function(done) { | |
var origOnError = window.onerror; | |
window.onerror = function() { | |
sandbox.restore(); | |
origOnError.apply(this, arguments); | |
}; | |
try { | |
fn.call(this, sandbox, function(err) { | |
sandbox.verifyAndRestore(); | |
done(err); | |
}); | |
} catch (e) { | |
sandbox.restore(); | |
throw e; | |
} | |
}; | |
} | |
else { | |
return function() { | |
var exception; | |
try { | |
fn.call(this, sandbox); | |
} | |
catch (e) { | |
exception = e; | |
} | |
if (exception) { | |
sandbox.restore(); | |
throw exception; | |
} | |
else { | |
sandbox.verifyAndRestore(); | |
} | |
}; | |
} | |
} | |
}; |
This file contains hidden or 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('Foo', function() { | |
beforeEach(function() { | |
this.foo = 'bar'; | |
}); | |
it('foo', mocha.sinon.test(function(sandbox, done) { | |
expect(this.foo).to.be('bar'); | |
sandbox.stub(window, 'confirm').returns(true); | |
setTimeout(function() { | |
expect(window.confirm()).to.ok(); | |
done(); | |
}, 1000); | |
})); | |
it('bar', mocha.sinon.test(function(sandbox) { | |
// confirm実行される(stubがリストアされてる) | |
expect(window.confirm()).to.ok(); | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment