question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to serve and load mocks / stubs for AJAX testing

See original GitHub issue

I use stubs to develop against when services aren’t available or are down. These stubs accurately reflect the data structure available from remote API’s in production and are used to provide data to the UI when developing wireframes.

These stubs should also be available to my unit tests for me to ensure that the UI is updated and reflects data in the stub the same way that they would be used in a development setting.

Currently, Karma only serves the specific files defined in karma.conf’s files configuration. Adding .json files generates script exceptions, but allows the server to serve them, however it also prevents the tests from running.

Ideally there should be a way to serve up files that can be accessed during test execution, without having those files included as scripts.

perhaps:

"mappings": {
    "stubs": "src/main/stubs/**/*.json"
}

Any request to /stubs/ provides the requested file.

For example:

var promise = $http({"url": "/stubs/myfile.json"});

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jayproulxcommented, Jan 20, 2014

These are all workarounds but don’t address the underlying issue which is: we may need to test arbitrary resource loading. The cleanest solution would be to say “I need these particular files, please serve them”, as I suggested above. Fixtures doesn’t resolve the issue of testing AJAX logic, this is a pretty straightforward fix, Karma already has similar syntax, just no way of serving arbitrary files.

If the list of files changes frequently, or the code that is being tested will automatically reach out to these files then we’re spending our time maintaining Karma and preprocessors, rather than focusing on the business logic that makes us money.

Since Karma is already responsible for serving files, the easiest solution is to have Karma serve the files, rather than needing to re-implement this solution every time we spin up a new project.

0reactions
kenglxncommented, Feb 28, 2014

I did what you suggested @vojtajina and created a preprocessor for json files (https://github.com/kenglxn/karma-json2js-preprocessor). It is a carbon copy of your html2js preprocessor.

After that exercise I realized that the html2js preprocessor would work fine for json as well, and all I really needed to do was to reuse you html2js preprocessor and define json files as well:

preprocessors: {
    '**/*.html': ['html2js'],
    '**/*.json': ['html2js']
},

after this the content of the json file is available from the html global.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for Spies, Stubs and Mocks in Sinon.js
In this test, we're using once and withArgs to define a mock which checks both the number of calls and the arguments given....
Read more >
How do I mock or stub an AJAX call that gets executed on load?
You could probably do it with $httpBackend . var $httpBackend; var yourMockedObject = {}; beforeEach(angular.mock.inject(function ...
Read more >
Sinon Tutorial: JavaScript Testing with Mocks, Spies & Stubs
In this Sinon tutorial, Jani Hartikainen demonstrates how to make unit testing non-trival JavaScript code trivial with the help of spies, ...
Read more >
How to test software: mocking, stubbing, and contract testing
In this friendly overview, explore the use of practices such as mocking, stubbing, and contract testing to get the right level of validation ......
Read more >
Sinon.JS - Standalone test fakes, spies, stubs and mocks for ...
Standalone test spies, stubs and mocks for JavaScript. Works with any unit testing framework.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found