Skip to content

Instantly share code, notes, and snippets.

@FilmKnurd
Created November 21, 2013 16:18
Show Gist options
  • Save FilmKnurd/7584726 to your computer and use it in GitHub Desktop.
Save FilmKnurd/7584726 to your computer and use it in GitHub Desktop.
// Install jquery-mockjax into vendor
// file: /app/index.html
<!-- @if tests=true -->
<link rel="stylesheet" href="/vendor/qunit/qunit/qunit.css">
<script src="/vendor/qunit/qunit/qunit.js"></script>
<script src="/vendor/jquery-mockjax/jquery.mockjax.js"></script>
// And add it to karma.conf
// file: karma.conf.js
files = [
...
'vendor/jquery-mockjax/jquery.mockjax.js',
...
]
// Create helper to stub endpoints
// file: /tests/test_helpers.js
function stubEndpointForHttpRequest(url, json) {
$.mockjax({
url: url,
dataType: 'json',
responseText: json
});
}
window.stubEndpointForHttpRequest = stubEndpointForHttpRequest;
// Configure mockjax
// file: /tests/helpers/start_app.js
window.$.mockjaxSettings.logging = false;
window.$.mockjaxSettings.responseTime = 0;
// Use in tests
// file: /tests/acceptance/some_test.js
test('some test', function(){
stubEndpointForHttpRequest('/api/some/route/here', {...});
expect(1);
visit('/').then(function(){
ok(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment