I want to use some dummy data in an angular unit test. The data is a large javascript array of objects. I'd rather not have this object in my unit test. Is there a way I can load this data into my unit test? In effect, I want the unit test to "include" another file which contains the data. Is this possible? Thanks
2 Answers
I created a globals.js file which I include in my karma.conf.js and it looks like this:
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: "..",
// frameworks to use
frameworks: ["jasmine"],
...
files: [
"test/unit/globals.js",
"app/js/*/**",
]
...
});
};
and in my globals.js file I just create the dummy data like this:
var globals = {
var1: 'mockValue1',
var2: 'mockValue2',
...
}
And in my unit tests I just write var something = globals.var1; if I want to use it there :)
Comments
You should use karma-ng-json2js-preprocessor.
It allows you to do exactly what you want. Have your JSONs in a separate .json file and then inject those mock jsons as angular constants into your tests.