Javascript – a good method of storing test data for development and unit testing with angularjs and jasmine

angularjsjavascriptunit testing

What is a good method of storing JSON data/objects for development and unit testing with angular and jasmine?

I have seen some ways of accomplishing this. However, since I am new to unit testing with JavaScript, I fear going in a bad direction.

Here are some examples of what I'd like to accomplish:

  • Let's say I want to develop my site off a set of test data and and then later on plug it into a database. I figure it would be easier if I am developing and testing off of the same data to start. Maybe all I have to do is reference a js file which has a method in it for returning an array of that data. I don't know what setbacks that might have.
  • Another example would be that my initial reaction when trying to make a test that uses data was to simply make a $http call to my .json file. I quickly learned I can't do that and would have to mock the data. It seems a pain to have my data both in a unit-test mock as well as a .json file when the current purpose of the data is just for test/dev. I'll have to manage it in two places.

Best Answer

If you start off with a spreadsheet, the process is easy:

  • Export the data as CSV
  • Transform the CSV to JSON
  • base64 Encode the JSON to reference it as a data:URI for HTTP tests

Once this is done, importing the data into your database is the last step.

References

Related Topic