Skip to content

Instantly share code, notes, and snippets.

@FilmKnurd
Created November 21, 2013 16:18
Show Gist options
  • Select an option

  • Save FilmKnurd/7584726 to your computer and use it in GitHub Desktop.

Select an option

Save FilmKnurd/7584726 to your computer and use it in GitHub Desktop.

Revisions

  1. FilmKnurd created this gist Nov 21, 2013.
    43 changes: 43 additions & 0 deletions EAK-mockjax
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // 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);
    });
    });