3

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.
  • 1
  • I updated the question, attempting to narrow the scope. – Chris May 13 '14 at 19:34
  • 2
    It is still fairly broad and too open for discussion. – Adam Zuckerman May 13 '14 at 21:37
  • Your title says angular (often meaning "angular 2+"), but your tag says angularjs. You should clarify which you are using so that you get better input. – Caleb Apr 27 '18 at 7:21
1

I would apply a single rule: keep your test data close to your test. Test is all about maintenance: they should be designed with maintenance in mind, hence, keep it simple.

Test data can be both input (context) and output (expected result) . Close to tests would be

  • directly into the test file (as an object defined in a variable or directly into the test function)
    • in the section/before if the json is to be used my multiple tests
    • in the test itself if the JSON is exclusive to the test
  • another file next to test file (in JSON or js file)

In terms of maintainability, you probably want to privilege the first use-case (single file). Both test and related data are available, providing all the information needed in a single place. Test file can grow huge but most code editors will help with navigating and collapsing JSON blocks. Reuse of JSON blocks when reused in different tests may look like a good idea from writing perspective. It nevertheless makes the tests less readable when adapting/correcting. I would advise avoiding optimizing test code in general and repeat the JSON structures per tests unless they are really big.

Testing JSON can be tricky though, do you expect exact same JSON, do you expect any kind of order to be respected in the data structure, do you expect your data to evolve and do you need to update your tests every time a single addition is made to data structure ? Where you put JSON is just the first question on the path to unit test and test driven development.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.