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!
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://warframe.wikia.com/wiki",
"pages": {
"mods_2.0": "/Mods_2.0",
"warframe_mods": "/Category:Warframe_Mods",
//more links follow
}
}
}
}
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.
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();
});
});
}
});
move it out of your before block:
describe("connection", () => {
const config = jsonfile.readFileSync(SCRAPER_CONFIG_FILE);
const wikiaUri = config.sources.wikia.link;
const wikiaPages = Object.values(config.sources.wikia.pages);
before(() => {
});
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();
});
});
}
});
That's because your loop wikiaPages
happens on declaration stage, which is before before
stage, so wikiaPages
is not yet assigned a value.
Unwrapping before
hook so reading config file and setting it
s happens on declaration stage should make it work:
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;
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();
});
});
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.