检查模式定义的验证要求是否在插入时捕获无效文档的测试总是失败,并显示一条指示验证失败的消息。如果捕获到无效文档,则测试旨在通过。
构建此测试的适当方法是什么?
曾考虑冒险进入 Collection2 的包测试,但我真的对证明该包有效不感兴趣。相反,我想验证我的架构是否正确构建以通过项目要求。
语境:
Windows 7
meteor@1.1.6
aldeed:autoform@5.4.0
aldeed:collection2@2.3.3
aldeed:simple-schema@1.3.3
velocity:core@0.9.3
sanjo:jasmine@0.16.4
要求:
1. Pulmonary Function test results (PFTs) are stored.
2. A pft document must contain a date (pftDate) and a Subject Id (subjId).
架构:
PFTs = new Meteor.Collection('pfts');
Schema = {};
Schema.PFTs = new SimpleSchema({
subjId: {
type: String,
autoform: {
type: "hidden",
label: false,
},
},
pftDate: {
type: Date,
label: 'Date',
max: function(){ return new Date() },
},
});
PFTs.attachSchema(Schema.PFTs);
服务器集成测试:
"use strict";
describe("PFTs", function(){
it("must be created with both subjId and pftDate set", function(){
var testDate = new Date();
var validNewPFT = {pftDate: testDate, subjId: '1'}
var invalidNewPFT = {};
// Fails.
// No std Jasmine matcher seems to recognize that
// the validation has caught the invalid document.
expect( PFTs.insert(invalidNewPFT) ).toThrow();
// Passes.
expect( PFTs.insert(validNewPFT) ).notToThrow();
});
});
速度测试结果:
Error: Subj is required
packages/aldeed:collection2/collection2.js:369:1: Error: Subj is required
at getErrorObject (packages/aldeed:collection2/collection2.js:369:1)
at [object Object].doValidate (packages/aldeed:collection2/collection2.js:352:1)
at [object Object].Mongo.Collection. (anonymous function) [as insert] (packages/aldeed:collection2/collection2.js:154:1)
at app\tests\jasmine\server\integration\pftDataModelSpec.js:8:18