How to use sandbox.stub
of Testing framework Sinon to overwrite e.g. navigator.language
or navigator.userAgent
for testing?
When I try to use the following:
suite('agent', function () {
var sandbox;
setup(function () {
// create sandbox environment for mocking about
sandbox = sinon.sandbox.create();
});
teardown(function () {
// restore the environment as it was before
sandbox.restore();
});
test('language', function () {
assert.equal(au.env.agent.language, navigator.language);
if (!navigator.language) assert.equal(au.env.agent.language, 'de');
var lang = "test_URK";
sandbox.stub(window.navigator, 'language', lang);
assert.equal(au.env.agent.language, lang);
});
});
then I'll get the following error: Cannot stub non-existent own property language:
Non of these stubs work as expected:
- sandbox.stub(window.navigator, 'language', lang);
- sandbox.stub(navigator, 'browserLanguage', lang);
are mocking the navigator object.
Any hints?