{"_id":"55c34683b6e05719008a046e","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":2,"parentDoc":null,"user":"55a4738a68c7e20d0036d4de","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"},"project":"55a92d71cf45e1390093f321","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-08-06T11:35:31.012Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":5,"body":"### Mocks\n\n#### Mocking Meteor\n\nThis package ships with mocks for the Meteor API. You can mock the Meteor API in your tests with:\n\n```javascript\nbeforeEach(function () {\n  MeteorStubs.install();\n});\n\nafterEach(function () {\n  MeteorStubs.uninstall();\n});\n```\n\nThis is done automatically for server unit tests. To disable on the server for certain packages set the environment variable `JASMINE_PACKAGES_TO_INCLUDE_IN_UNIT_TESTS`. For example\n\n    export JASMINE_PACKAGES_TO_INCLUDE_IN_UNIT_TESTS=dburles:factory \n\nYou need to do it yourself for your client tests if you want to write\nunit tests that run in the browser.\n\n#### Mocking objects\n\nYou can mock any object with the global helper function `mock`.\nThis will avoid unwanted side effects that can affect other tests.\nHere is an example how to mock the Players collection of the Leaderboard example:\n\n```javascript\nbeforeEach(function () {\n  mock(window, 'Players');\n});\n```\n\nThis will mock the Players collection for each test.\nThe original Players collection is automatically restored after each test.\n\n#### Creating more complex mocks\n\nYou can also create mocks manually. I would suggest to use the following pattern:\n\nCreate a mock service with a method `install` and `uninstall` ([example for Meteor](https://github.com/alanning/meteor-stubs/blob/master/index.js))\n\n  * install: Saves the original object and mocks it\n  * uninstall: Restores the original object\n\nThis pattern allows you to enable mocks only for specific tests and have a clean and independent mock for each test.","excerpt":"Advanced features of the server unit mode","slug":"jasmine-server-unit-mode","type":"basic","title":"Server unit mode"}

Server unit mode

Advanced features of the server unit mode

### Mocks #### Mocking Meteor This package ships with mocks for the Meteor API. You can mock the Meteor API in your tests with: ```javascript beforeEach(function () { MeteorStubs.install(); }); afterEach(function () { MeteorStubs.uninstall(); }); ``` This is done automatically for server unit tests. To disable on the server for certain packages set the environment variable `JASMINE_PACKAGES_TO_INCLUDE_IN_UNIT_TESTS`. For example export JASMINE_PACKAGES_TO_INCLUDE_IN_UNIT_TESTS=dburles:factory You need to do it yourself for your client tests if you want to write unit tests that run in the browser. #### Mocking objects You can mock any object with the global helper function `mock`. This will avoid unwanted side effects that can affect other tests. Here is an example how to mock the Players collection of the Leaderboard example: ```javascript beforeEach(function () { mock(window, 'Players'); }); ``` This will mock the Players collection for each test. The original Players collection is automatically restored after each test. #### Creating more complex mocks You can also create mocks manually. I would suggest to use the following pattern: Create a mock service with a method `install` and `uninstall` ([example for Meteor](https://github.com/alanning/meteor-stubs/blob/master/index.js)) * install: Saves the original object and mocks it * uninstall: Restores the original object This pattern allows you to enable mocks only for specific tests and have a clean and independent mock for each test.