1

检查模式定义的验证要求是否在插入时捕获无效文档的测试总是失败,并显示一条指示验证失败的消息。如果捕获到无效文档,则测试旨在通过​​。

构建此测试的适当方法是什么?

曾考虑冒险进入 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
4

3 回答 3

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;
    });
  });
});
于 2015-08-10T19:10:42.640 回答
0

@Sanjo 感谢您的指导。

以下解决了提出的问题:

"use strict";
describe("PFTs", function(){
  it("cannot be created without a subjId", function(){
    var testDate = new Date();
    var invalidNewPFT =   {pftDate: testDate}
    var preinsertCount = PFTs.find().fetch().length;

    // expect used here to swallow the msg sent to the browser by Collection2
    expect( function(){ PFTs.insert(invalidNewPFT); }).toThrow;

    // Verify that a record was not saved
    expect( PFTs.find().fetch().length ).toEqual(preinsertCount);
  });
});

第一个期望吞下由 Collection2 发送到浏览器的消息。有趣的是,使用 .toThrow 或 .not.toThrow 都没有关系,效果是一样的。真正的测试是检查 PFT 文档的数量是否增加了一个。

于 2015-08-10T02:47:51.313 回答
0

您必须传递一个函数以期望您期望抛出:

expect(function () { PFTs.insert(invalidNewPFT); }).toThrow();

expect(function () { PFTs.insert(validNewPFT); }).not.toThrow();

如您所见,它.not.toThrow()不是notToThrow().

于 2015-08-09T22:39:54.577 回答