{"action":"created","issue":{"url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/249","labels_url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/249/labels{/name}","comments_url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/249/comments","events_url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/249/events","html_url":"https://github.com/aldeed/meteor-collection2/issues/249","id":99967951,"number":249,"title":"Collection2 and Simple-Schema seem to complicate testing with Velocity and Jasmine","user":{"login":"dpneumo","id":1223637,"avatar_url":"https://avatars.githubusercontent.com/u/1223637?v=3","gravatar_id":"","url":"https://api.github.com/users/dpneumo","html_url":"https://github.com/dpneumo","followers_url":"https://api.github.com/users/dpneumo/followers","following_url":"https://api.github.com/users/dpneumo/following{/other_user}","gists_url":"https://api.github.com/users/dpneumo/gists{/gist_id}","starred_url":"https://api.github.com/users/dpneumo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dpneumo/subscriptions","organizations_url":"https://api.github.com/users/dpneumo/orgs","repos_url":"https://api.github.com/users/dpneumo/repos","events_url":"https://api.github.com/users/dpneumo/events{/privacy}","received_events_url":"https://api.github.com/users/dpneumo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-08-10T04:24:49Z","updated_at":"2015-08-10T17:15:36Z","closed_at":null,"body":"A test to check that schema defined validation requirement catches an invalid document on insert always fails with a message indicating a validation failure. The test is intended to pass if the invalid document is caught.\r\n\r\n    Error: Subj is required\r\n    packages/aldeed:collection2/collection2.js:369:1: Error: Subj is required\r\n      at getErrorObject (packages/aldeed:collection2/collection2.js:369:1)\r\n      at [object Object].doValidate (packages/aldeed:collection2/collection2.js:352:1)\r\n      at [object Object].Mongo.Collection. (anonymous function) [as insert] (packages/aldeed:collection2/collection2.js:154:1)\r\n      at app\\tests\\jasmine\\server\\integration\\pftDataModelSpec.js:8:18\r\n\r\nThe Server Integration tests used:\r\n\r\n    \"use strict\";\r\n    describe(\"PFTs\", function(){\r\n      it(\"must be created with subjId set\", function(){\r\n        var invalidNewPFT = {};\r\n        expect( PFTs.insert(invalidNewPFT) ).toThrow();\r\n      });\r\n    });\r\n\r\nThe question of how to test this was posed on StackOverflow. @Sanjo recommended I use:\r\n\r\n    expect(function () { PFTs.insert(invalidNewPFT); }).toThrow();\r\n\r\nThis still failed with the same message indicating that validation failed. Based on that recommendation I found this code does work to achieve my testing goals:\r\n\r\n    \"use strict\";\r\n    describe(\"PFTs\", function(){\r\n      it(\"cannot be created without a subjId\", function(){\r\n        var testDate = new Date();\r\n        var invalidNewPFT =   {pftDate: testDate}\r\n        var preinsertCount = PFTs.find().fetch().length;\r\n\r\n        // expect used here to swallow the msg sent to the browser by Collection2\r\n        expect( function(){ PFTs.insert(invalidNewPFT); }).toThrow;\r\n\r\n        // Verify that a record was not saved\r\n        expect( PFTs.find().fetch().length ).toEqual(preinsertCount);\r\n      });\r\n    });\r\n\r\nHowever, it does not matter whether I use .toThrow or .not.toThrow in the first expect. It appears that Collection2 is swallowing the error generated by attempting to insert an invalid record. Perhaps this occurs by submitting the insert within Collection2 with a callback added that captures the error or perhaps the call to insert is never made. In any event it is necessary to step around this by checking for an increment in the number of documents in the collection after the insert is attempted. This only means two extra lines of code. However, it is a little ugly to have to do this and also it was not obvious that I could use expect to swallow the message from Collection2. Without that step the actual check for a document count increase would never occur.\r\n\r\nIt might be appropriate to either document this behavior or to provide a mechanism to allow the validation failure to flow out to the testing harness in a manner that is easily testable using standard Jasmine matchers.  \r\n\r\nContext: \r\n\r\n    Windows 7\r\n    meteor@1.1.6\r\n    aldeed:autoform@5.4.0\r\n    aldeed:collection2@2.3.3\r\n    aldeed:simple-schema@1.3.3\r\n    velocity:core@0.9.3\r\n    sanjo:jasmine@0.16.4\r\n\r\nRequirements:\r\n\r\n    1. Pulmonary Function test results (PFTs) are stored.\r\n    2. A pft document must contain a date (pftDate) and a Subject Id (subjId).\r\n\r\nSchema:\r\n\r\n    PFTs = new Meteor.Collection('pfts');\r\n\r\n    Schema = {};\r\n    Schema.PFTs =  new SimpleSchema({\r\n      subjId: {\r\n        type: String,\r\n        autoform: {\r\n          type: \"hidden\",\r\n          label: false,\r\n        },\r\n      },\r\n      pftDate: {\r\n        type: Date,\r\n        label: 'Date',\r\n        max: function(){ return new Date() },\r\n      },\r\n    });\r\n\r\n    PFTs.attachSchema(Schema.PFTs);\r\n"},"comment":{"url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/comments/129531090","html_url":"https://github.com/aldeed/meteor-collection2/issues/249#issuecomment-129531090","issue_url":"https://api.github.com/repos/aldeed/meteor-collection2/issues/249","id":129531090,"user":{"login":"Sanjo","id":727955,"avatar_url":"https://avatars.githubusercontent.com/u/727955?v=3","gravatar_id":"","url":"https://api.github.com/users/Sanjo","html_url":"https://github.com/Sanjo","followers_url":"https://api.github.com/users/Sanjo/followers","following_url":"https://api.github.com/users/Sanjo/following{/other_user}","gists_url":"https://api.github.com/users/Sanjo/gists{/gist_id}","starred_url":"https://api.github.com/users/Sanjo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sanjo/subscriptions","organizations_url":"https://api.github.com/users/Sanjo/orgs","repos_url":"https://api.github.com/users/Sanjo/repos","events_url":"https://api.github.com/users/Sanjo/events{/privacy}","received_events_url":"https://api.github.com/users/Sanjo/received_events","type":"User","site_admin":false},"created_at":"2015-08-10T17:15:36Z","updated_at":"2015-08-10T17:15:36Z","body":"May you could test it like this:\r\n\r\n1. Test that the collection has the schema assigned\r\n2. Test the validation of data directly with the Simple Schema (Schema.PFTs)\r\n\r\nI think the other test concerns should be tests in the collections2 package."}}