检查模式定义的验证要求是否在插入时捕获无效文档的测试始终失败,并显示一条指示验证失败的消息。如果捕获了无效文档,则该测试旨在通过。
构建此测试的适当方法是什么?
已经考虑过冒险进入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
答案 0 :(得分:1)
GitHub问题下的讨论产生了以下解决方案:
"use strict";
describe("The PFT Schema", function(){
it("contains keys for subjId and pftDate", function(){
var schemaKeys = PFTs._c2._simpleSchema._firstLevelSchemaKeys;
expect(schemaKeys).toContain('subjId');
expect(schemaKeys).toContain('pftDate');
});
describe("context", function(){
var ssPFTContext = Schema.PFTs.namedContext("pft");
it("requires the presence of subjId & pftDate", function(){
var validPFTData = {subjId: 1, pftDate: new Date()};
expect( ssPFTContext.validate(validPFTData) ).toBeTrue;
});
it("fails if subjId is absent", function(){
var invalidPFTData = {pftDate: new Date()};
expect( ssPFTContext.validate(invalidPFTData) ).toBeFalse;
});
it("fails if pftDate is absent", function(){
var invalidPFTData = {subjId: 1};
expect( ssPFTContext.validate(invalidPFTData) ).toBeFalse;
});
});
});
答案 1 :(得分:0)
您必须传递一个函数,以期望您抛出:
expect(function () { PFTs.insert(invalidNewPFT); }).toThrow();
expect(function () { PFTs.insert(validNewPFT); }).not.toThrow();
您可以看到它是.not.toThrow()
而不是notToThrow()
。
答案 2 :(得分:0)
@Sanjo感谢您的指导。
以下解决了提出的问题:
$jobnumber= $_REQUEST['jobnumber'];
第一个期望吞下Collection2发送给浏览器的消息。有趣的是,无论是使用.toThrow还是.not.toThrow,效果都是一样的。真正的考验是检查PFT文件数量增加一个。