{"_id":"55bf690cad601c2b00762d06","project":"55a92d71cf45e1390093f321","user":"55a4738a68c7e20d0036d4de","parentDoc":null,"version":{"_id":"55a92d71cf45e1390093f324","__v":12,"project":"55a92d71cf45e1390093f321","createdAt":"2015-07-17T16:29:37.746Z","releaseDate":"2015-07-17T16:29:37.746Z","categories":["55a92d72cf45e1390093f325","55b9aea1baf4cd2300bc56e7","55bf16593179dd1900a19521","55bf16b93179dd1900a19525","55bf16c53bbd4b17002c2369","55bf234703d092210084c07b","55bf2859ad601c2b00762c4c","55bf3217ad601c2b00762c69","55bf321b03d092210084c09d","55bf5ef78b83a5370044504e","55c345418713ec1900d1732d","55c3473057f7f32d0016ec0a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":1,"category":{"_id":"55c345418713ec1900d1732d","project":"55a92d71cf45e1390093f321","version":"55a92d71cf45e1390093f324","__v":2,"pages":["55c345838713ec1900d17330","55c34683b6e05719008a046e"],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-08-06T11:30:09.092Z","from_sync":false,"order":2,"slug":"jasmine-advanced","title":"Jasmine Advanced"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-08-03T13:13:48.069Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"When you write integration tests, they will run in an isolated instance of your app with an empty database. Usually you want to create some sample data in your database that you use in your integration tests. This can be achieved with database fixtures. It's a script that sets up your database for integration tests.\n\n## Server integration test fixtures\n\nUse this script to get started: https://gist.github.com/Sanjo/edbc0c1848de726b78d5\n\nCopy it to a file in `/tests/jasmine/server/integration/` for server integration.\n\n## Client integration test fixtures\n\nCreate file `/tests/jasmine/client/integration/database-fixture.js` with this content:\n\n```\nMeteor.call('clearDB', function(){\n  Meteor.call('loadFixtures');\n});\n```\n\nAdd to server side script with code based on this script: https://gist.github.com/qnub/97d828f11c677007cb07\n\n## Fixtures via Package\n\nAnother pattern for creating fixtures is to do the following inside your app:\n```bash\nmeteor create --package fixtures\n```\n\nThen modify the `package.js` file to set the `debugOnly` flag to true like this:\n```javascript\nPackage.describe({\n  name: 'fixtures',\n  version: '0.0.1',\n  debugOnly: true,\n  // ...\n});\n```\nThe `debugOnly` flag instruct Meteor not to bundle this package when building, which is how you\nensure this package does not make it to production. You can now define all your fixtures in this\npackage.\n\nNow add some Meteor methods to the package that will create your fixture data. You can call this Meteor methods from your tests.\n\n__fixtures.js__:\n\n```javascript\nvar createUser = function (userData) {\n  var user = Meteor.users.findOne({username: userData.username});\n\n  if (!user) {\n    var userId = Accounts.createUser(userData);\n    user = Meteor.users.findOne(userId);\n  }\n\n  return user;\n};\n\nMeteor.methods({\n  'fixtures/users/create': createUser\n});\n```\n\nYou can find a full example of a fixtures package [here](https://github.com/Sanjo/SpaceTalk/tree/feature/testing/packages/testing).","excerpt":"","slug":"jasmine-database-fixtures-for-integration-tests","type":"basic","title":"Database Fixtures for Integration Tests"}

Database Fixtures for Integration Tests


When you write integration tests, they will run in an isolated instance of your app with an empty database. Usually you want to create some sample data in your database that you use in your integration tests. This can be achieved with database fixtures. It's a script that sets up your database for integration tests. ## Server integration test fixtures Use this script to get started: https://gist.github.com/Sanjo/edbc0c1848de726b78d5 Copy it to a file in `/tests/jasmine/server/integration/` for server integration. ## Client integration test fixtures Create file `/tests/jasmine/client/integration/database-fixture.js` with this content: ``` Meteor.call('clearDB', function(){ Meteor.call('loadFixtures'); }); ``` Add to server side script with code based on this script: https://gist.github.com/qnub/97d828f11c677007cb07 ## Fixtures via Package Another pattern for creating fixtures is to do the following inside your app: ```bash meteor create --package fixtures ``` Then modify the `package.js` file to set the `debugOnly` flag to true like this: ```javascript Package.describe({ name: 'fixtures', version: '0.0.1', debugOnly: true, // ... }); ``` The `debugOnly` flag instruct Meteor not to bundle this package when building, which is how you ensure this package does not make it to production. You can now define all your fixtures in this package. Now add some Meteor methods to the package that will create your fixture data. You can call this Meteor methods from your tests. __fixtures.js__: ```javascript var createUser = function (userData) { var user = Meteor.users.findOne({username: userData.username}); if (!user) { var userId = Accounts.createUser(userData); user = Meteor.users.findOne(userId); } return user; }; Meteor.methods({ 'fixtures/users/create': createUser }); ``` You can find a full example of a fixtures package [here](https://github.com/Sanjo/SpaceTalk/tree/feature/testing/packages/testing).