最近、MongoDBラッパーとしてMongooseを使用して、NodeJSでの優れたテスト駆動開発に頭を悩ませようとしました。
私の質問は、私がそれを正しくやっているかどうかです。私は正しいものをテストしていますか、空の値などをカバーするテストをもっと行う必要があります。私が改善できることは何だと思いますか?自由に批判してください。
テスト:
var assert = require("assert"),
mongoose = require("mongoose"),
sugar = require("sugar"),
Factory = require("factory-lady"),
Timekeeper = require("timekeeper"),
Link = require("./../app/models/link");
mongoose.connect("mongodb://localhost/link_test");
Factory.define("link", Link, {
createdAt: new Date("2012-04-04"),
title: "Example",
updatedAt: new Date("2012-04-05"),
url: "http://www.example.com"
});
describe("Link", function() {
describe("validation for", function() {
["url", "title"].forEach(function(property) {
it("must have a " + property, function(done) {
Factory.build("link", function(link) {
link[property] = null;
link.validate(function(err) {
assert.notEqual(err, null);
assert.notEqual(err.errors[property], null);
done();
});
});
});
});
it("should default the title to url if missing", function(done) {
Factory.build("link", function(link) {
link.title = undefined;
link.validate(function(err) {
assert.equal(link.title, link.url);
done();
});
});
});
describe("url", function() {
["http://example.com", "http://www.example.com",
"http://nodejs.org/api/assert.html#assert_assert_deepequal_actual_expected_message"].forEach(function(url) {
it(url + " should be valid url", function(done) {
Factory.build("link", { url: url }, function(link) {
link.validate(function(err) {
assert.equal(err, null);
done();
});
});
});
});
["Invalid url", "example.com"].forEach(function(url) {
it(url + " should be invalid url", function(done) {
Factory.build("link", { url: url }, function(link) {
link.validate(function(err) {
assert.notEqual(err, null);
assert.notEqual(err.errors.url, null);
done();
});
});
});
});
});
describe("missing createdAt or updatedAt", function() {
beforeEach(function() {
Timekeeper.freeze(new Date());
});
afterEach(function() {
Timekeeper.reset();
});
["createdAt", "updatedAt"].forEach(function(property) {
it("should default the " + property + " to now if missing", function(done) {
Factory.build("link", function(link) {
link[property] = undefined;
link.validate(function(err) {
assert.deepEqual(link[property], new Date());
done();
});
});
});
});
["createdAt", "updatedAt"].forEach(function(property) {
it("should leave the " + property + " unchanged if it's given", function(done) {
Factory.build("link", function(link) {
var date = new Date("2012-01-01");
link[property] = date;
link.validate(function(err) {
assert.deepEqual(link[property], date);
done();
});
});
});
});
it("should default the updatedAt to now if missing when performing an update", function(done) {
Factory("link", function(link) {
link.validate(function(err) {
assert.equal(link.updatedAt, new Date());
done();
});
});
});
it("should leave the updatedAt unchanged if it's given when performing an update", function(done) {
Factory("link", function(link) {
var date = new Date("2012-01-01");
link.updatedAt = date;
link.validate(function(err) {
assert.deepEqual(link.updatedAt, date);
done();
});
});
});
});
});
});
モデル:
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
var linkSchema = new Schema({
createdAt: { type: Date, required: true },
title: { type: String, required: true },
updatedAt: { type: Date, required: true },
url: { type: String, required: true, validate: [validUrl, "invalid url"] }
});
function validUrl(url) {
if ((url instanceof String || typeof url == 'string') && url.length) {
return url.match(/^\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))$/i);
}
}
linkSchema.pre("validate", function (next) {
if (typeof this.title === "undefined" && typeof this.url !== "undefined") {
this.title = this.url;
}
if (typeof this.createdAt === "undefined") {
this.createdAt = new Date();
}
if (typeof this.updatedAt === "undefined" || !this.isModified("updatedAt")) {
this.updatedAt = new Date();
}
next();
});
module.exports = mongoose.model("Link", linkSchema);