Background
I am scrapping a wikia for information, and before I actually do that, I wanna make sure I can connect to the wikia servers.
Best way to do it? Using mocha tests with my nodejs app!
Objective
I have a configuration file, which has an object with all the links I want. I have a battery set called "connection" and I want each test to try to connect to the wikia.
{
"sources": {
"wikia": {
"link": "http://ift.tt/2jKTXk5",
"pages": {
"mods_2.0": "/Mods_2.0",
"warframe_mods": "/Category:Warframe_Mods",
//more links follow
}
}
}
}
Problem
The problem here is that I don't want to write and replicate the same test for over a dozen wikia pages. I want to avoid repetition.
My solution to this, was to put every it
inside a loop, however, the code breaks because my wikiaPages
arrays is always undefined, even when I use the before()
function.
Code
let assert = require("assert");
let superagent = require("superagent");
let jsonfile = require("jsonfile");
const SCRAPER_CONFIG_FILE = "./scraperConfig.json";
describe("connection", () => {
let wikiaUri;
let wikiaPages;
let completeUri;
before(() => {
let config = jsonfile.readFileSync(SCRAPER_CONFIG_FILE);
wikiaUri = config.sources.wikia.link;
wikiaPages = Object.values(config.sources.wikia.pages);
});
for(let pageUri of wikiaPages) {
completeUri = wikiaUri + pageUri;
it("connects to " + completeUri, done => {
superagent.get(completeUri, (error, res) => {
assert.ifError(error);
assert.equal(res.status, 200);
done();
});
});
}
});
Questions
- How can I fix this code, so it works ?
Aucun commentaire:
Enregistrer un commentaire