【问题标题】:schema acceptance testing with meteor, velocity and jasmine使用流星、速度和茉莉花进行模式验收测试
【发布时间】:2015-08-11 03:10:42
【问题描述】:

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

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

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

【问题讨论】:

    标签: meteor jasmine integration-testing velocity meteor-collection2


    【解决方案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;
        });
      });
    });
    

    【讨论】:

      【解决方案2】:

      你必须传递一个期望你期望抛出的函数:

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

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

      【讨论】:

      • 感谢您惊人的快速响应。看来这不起作用。即使插入失败,Collection.insert 也会返回一个 id。所以我在 Velocity 中收到了这条消息:TypeError: Object [object Object] has no method 'ToThrow' btw: 我将 notToThrow 中的错字更正为 .not.toThrow
      • 也许我应该进行插入,然后测试是否存在具有返回 id 的文档。似乎有点令人费解,但考虑到 collection.insert 的返回,这可能是最好的方法。我认为可能没有其他可以匹配的。
      • 不。 ` "使用严格"; describe("PFTs", function(){ it("必须同时设置 subjId 和 pftDate 创建", function(){ var testDate = new Date(); var validNewPFT = {pftDate: testDate, subjId: '1'} var invalidNewPFT = {}; var invalidPftId = PFTs.insert(invalidNewPFT);expect(PFTs.findOne(invalidPftId)).toBeUndefined();var validPftId = PFTs.insert(validNewPFT);expect(PFTs.findOne(validPftId))。被定义为(); }); }); ` 再次得到原始投诉:错误:需要主题 抱歉格式化。 mini-Markdown 和我相处不来。
      • 抱歉上面的格式。 mini-Markdown 和我相处不来。
      【解决方案3】:

      @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 文档的数量是否增加了 1。

      【讨论】:

        猜你喜欢
        • 2014-09-26
        • 2015-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-02
        • 1970-01-01
        • 2020-08-19
        • 2016-10-13
        相关资源
        最近更新 更多