{"id":1740,"date":"2013-01-12T07:53:16","date_gmt":"2013-01-12T07:53:16","guid":{"rendered":"http:\/\/thekua.com\/atwork\/?p=1740"},"modified":"2019-07-25T10:15:23","modified_gmt":"2019-07-25T08:15:23","slug":"cheat-sheet-for-javascript-testing-with-jasmine","status":"publish","type":"post","link":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/","title":{"rendered":"Cheat Sheet for Javascript Testing with Jasmine"},"content":{"rendered":"<p>Jasmine is the default unit testing framework that I use when writing javascript, however my poor brain can&#8217;t always remember all the different ways of getting things to work. There are quite a number of cheat sheets out on the internet including:<\/p>\n<ul>\n<li><a href=\"https:\/\/web.archive.org\/web\/20121102174556\/http:\/\/cheat.errtheblog.com:80\/s\/jasmine\" title=\"Err the Blog Cheat Sheet for Jasmine\">Err the Blog&#8217;s Cheat Sheet<\/a><\/li>\n<li><a href=\"http:\/\/tobyho.com\/2011\/12\/15\/jasmine-spy-cheatsheet\/\" title=\"Toby Ho Jasmine Cheat Sheet\">Toby Ho&#8217;s Cheat Sheet<\/a><\/li>\n<\/ul>\n<p>They don&#8217;t quite cover all the examples as well. Here&#8217;s my contributions to demonstrate some of the common uses.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">describe(&amp;quot;jasmine&amp;quot;, function () {\r\n\r\n    describe(&amp;quot;basic invocations&amp;quot;, function () {\r\n\r\n        var SampleDependency = function () {\r\n            return {\r\n                usefulMethod:function (firstParameter, secondParameter) {\r\n                },\r\n                anotherUsefulMethod:function () {\r\n                }\r\n            };\r\n        };\r\n\r\n        var Consumer = function (dependency) {\r\n            return {\r\n                run:function () {\r\n                    dependency.usefulMethod(&amp;quot;first&amp;quot;, &amp;quot;second&amp;quot;);\r\n                },\r\n                runSecondMethod:function () {\r\n                    dependency.anotherUsefulMethod();\r\n                },\r\n                runWithRequiredCallback:function(callback) {\r\n                    callback(&amp;quot;an argument&amp;quot;);\r\n                }\r\n            };\r\n        };\r\n\r\n        it(&amp;quot;should spy on an existing function&amp;quot;, function () {\r\n            \/\/ given\r\n            var dependency = new SampleDependency();\r\n            spyOn(dependency, &amp;quot;usefulMethod&amp;quot;);\r\n            var consumer = new Consumer(dependency);\r\n\r\n            \/\/ when\r\n            consumer.run();\r\n\r\n            \/\/ then\r\n            expect(dependency.usefulMethod).toHaveBeenCalled();\r\n            expect(dependency.usefulMethod).toHaveBeenCalledWith(&amp;quot;first&amp;quot;, &amp;quot;second&amp;quot;);\r\n            expect(dependency.usefulMethod).toHaveBeenCalledWith(jasmine.any(String), jasmine.any(String));\r\n            expect(dependency.usefulMethod.callCount).toEqual(1);\r\n            expect(dependency.usefulMethod.mostRecentCall.args).toEqual(&#x5B;&amp;quot;first&amp;quot;, &amp;quot;second&amp;quot;]);\r\n        });\r\n\r\n        it(&amp;quot;should demonstrate resetting of the spy&amp;quot;, function () {\r\n            \/\/ given\r\n            var dependency = new SampleDependency();\r\n            spyOn(dependency, &amp;quot;usefulMethod&amp;quot;);\r\n            dependency.usefulMethod();\r\n            dependency.usefulMethod.reset();\r\n\r\n            \/\/ when\r\n            dependency.usefulMethod();\r\n\r\n            \/\/ then\r\n            expect(dependency.usefulMethod).toHaveBeenCalled();\r\n            expect(dependency.usefulMethod.callCount).toEqual(1);\r\n        });\r\n\r\n        it(&amp;quot;should demonstrate creating a spy object with prepopulated methods&amp;quot;, function () {\r\n            \/\/ given\r\n            var dependency = jasmine.createSpyObj(&amp;quot;dependency&amp;quot;, &#x5B;&amp;quot;usefulMethod&amp;quot;, &amp;quot;anotherUsefulMethod&amp;quot;]);\r\n            var consumer = new Consumer(dependency);\r\n\r\n            \/\/ when\r\n            consumer.run();\r\n            consumer.runSecondMethod();\r\n\r\n            \/\/ then\r\n            expect(dependency.usefulMethod).toHaveBeenCalled();\r\n            expect(dependency.anotherUsefulMethod).toHaveBeenCalled();\r\n        });\r\n\r\n        it(&amp;quot;should demonstrate creating a stub object&amp;quot;, function () {\r\n            \/\/ given\r\n            var dependency = jasmine.createSpyObj(&amp;quot;dependency&amp;quot;, &#x5B;&amp;quot;usefulMethod&amp;quot;, &amp;quot;anotherUsefulMethod&amp;quot;]);\r\n            var consumer = new Consumer(dependency);\r\n            var stubbedCallback = jasmine.createSpy(&amp;quot;stub callback&amp;quot;);\r\n\r\n\r\n            \/\/ when\r\n            consumer.runWithRequiredCallback(stubbedCallback);\r\n\r\n            \/\/ then\r\n            expect(stubbedCallback).toHaveBeenCalled();\r\n            expect(stubbedCallback).toHaveBeenCalledWith(&amp;quot;an argument&amp;quot;);\r\n        });\r\n    });\r\n\r\n\r\n    describe(&amp;quot;returning a value&amp;quot;, function () {\r\n        var Dependency = function () {\r\n            return {\r\n                getMultiplier:function () {\r\n                    return 10;\r\n                }\r\n            };\r\n        };\r\n\r\n        var Consumer = function (dependency) {\r\n            return {\r\n                calculateSomethingWithMultiplier:function (number) {\r\n                    return number * dependency.getMultiplier();\r\n                }\r\n            };\r\n        };\r\n\r\n        it('should be correct', function () {\r\n            \/\/ given\r\n            var dependency = new Dependency();\r\n            spyOn(dependency, &amp;quot;getMultiplier&amp;quot;).andReturn(40);\r\n            var consumer = new Consumer(dependency);\r\n\r\n            \/\/ when\r\n            var result = consumer.calculateSomethingWithMultiplier(3);\r\n\r\n            \/\/ then\r\n            expect(result).toEqual(120);\r\n        });\r\n    });\r\n\r\n\r\n    it(&amp;quot;should demonstrate creating a stub that returns a value&amp;quot;, function () {\r\n    });\r\n\r\n    describe(&amp;quot;creating a stub that calls a fake&amp;quot;, function () {\r\n        var Dependency = function () {\r\n            return {\r\n                request:function (callback) {\r\n                }\r\n            };\r\n        };\r\n        var Consumer = function (dependency) {\r\n            var capturedValue = &amp;quot;&amp;quot;;\r\n            return {\r\n                hardAtWork:function () {\r\n                    dependency.request(function (value) {\r\n                        capturedValue = value;\r\n                    });\r\n                },\r\n                getCapturedValue:function () {\r\n                    return capturedValue;\r\n                }\r\n            };\r\n        };\r\n\r\n        it('should demonstrate creating a stub function that does something interesting', function () {\r\n            \/\/ given\r\n            var dependency = new Dependency();\r\n            spyOn(dependency, &amp;quot;request&amp;quot;).andCallFake(function (callback) {\r\n                callback(&amp;quot;Controlled return value from callback&amp;quot;);\r\n            });\r\n            var consumer = new Consumer(dependency);\r\n\r\n            \/\/ when\r\n            consumer.hardAtWork();\r\n\r\n            \/\/ then\r\n            expect(consumer.getCapturedValue()).toEqual(&amp;quot;Controlled return value from callback&amp;quot;);\r\n        });\r\n\r\n    });\r\n});<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Jasmine is the default unit testing framework that I use when writing javascript, however my poor brain can&#8217;t always remember all the different ways of getting things to work. There are quite a number of cheat sheets out on the internet including: Err the Blog&#8217;s Cheat Sheet Toby Ho&#8217;s Cheat Sheet They don&#8217;t quite cover [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,47],"tags":[],"class_list":["post-1740","post","type-post","status-publish","format-standard","hentry","category-development","category-javascript","post-preview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Cheat Sheet for Javascript Testing with Jasmine - patkua@work<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cheat Sheet for Javascript Testing with Jasmine - patkua@work\" \/>\n<meta property=\"og:description\" content=\"Jasmine is the default unit testing framework that I use when writing javascript, however my poor brain can&#8217;t always remember all the different ways of getting things to work. There are quite a number of cheat sheets out on the internet including: Err the Blog&#8217;s Cheat Sheet Toby Ho&#8217;s Cheat Sheet They don&#8217;t quite cover [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/\" \/>\n<meta property=\"og:site_name\" content=\"patkua@work\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-12T07:53:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-07-25T08:15:23+00:00\" \/>\n<meta name=\"author\" content=\"Patrick\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@patkua\" \/>\n<meta name=\"twitter:site\" content=\"@patkua\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Patrick\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/\"},\"author\":{\"name\":\"Patrick\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"headline\":\"Cheat Sheet for Javascript Testing with Jasmine\",\"datePublished\":\"2013-01-12T07:53:16+00:00\",\"dateModified\":\"2019-07-25T08:15:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/\"},\"wordCount\":563,\"commentCount\":0,\"articleSection\":[\"Development\",\"Javascript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/\",\"name\":\"Cheat Sheet for Javascript Testing with Jasmine - patkua@work\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\"},\"datePublished\":\"2013-01-12T07:53:16+00:00\",\"dateModified\":\"2019-07-25T08:15:23+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2013\\\/01\\\/cheat-sheet-for-javascript-testing-with-jasmine\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cheat Sheet for Javascript Testing with Jasmine\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\",\"name\":\"patkua@work\",\"description\":\"The intersection of technology and leadership\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\",\"name\":\"Patrick\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"caption\":\"Patrick\"},\"sameAs\":[\"http:\\\/\\\/thekua.com\\\/atwork\"],\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/author\\\/patrick\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cheat Sheet for Javascript Testing with Jasmine - patkua@work","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/","og_locale":"en_US","og_type":"article","og_title":"Cheat Sheet for Javascript Testing with Jasmine - patkua@work","og_description":"Jasmine is the default unit testing framework that I use when writing javascript, however my poor brain can&#8217;t always remember all the different ways of getting things to work. There are quite a number of cheat sheets out on the internet including: Err the Blog&#8217;s Cheat Sheet Toby Ho&#8217;s Cheat Sheet They don&#8217;t quite cover [&hellip;]","og_url":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/","og_site_name":"patkua@work","article_published_time":"2013-01-12T07:53:16+00:00","article_modified_time":"2019-07-25T08:15:23+00:00","author":"Patrick","twitter_card":"summary_large_image","twitter_creator":"@patkua","twitter_site":"@patkua","twitter_misc":{"Written by":"Patrick","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/#article","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/"},"author":{"name":"Patrick","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"headline":"Cheat Sheet for Javascript Testing with Jasmine","datePublished":"2013-01-12T07:53:16+00:00","dateModified":"2019-07-25T08:15:23+00:00","mainEntityOfPage":{"@id":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/"},"wordCount":563,"commentCount":0,"articleSection":["Development","Javascript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/","url":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/","name":"Cheat Sheet for Javascript Testing with Jasmine - patkua@work","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/#website"},"datePublished":"2013-01-12T07:53:16+00:00","dateModified":"2019-07-25T08:15:23+00:00","author":{"@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"breadcrumb":{"@id":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thekua.com\/atwork\/2013\/01\/cheat-sheet-for-javascript-testing-with-jasmine\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thekua.com\/atwork\/"},{"@type":"ListItem","position":2,"name":"Cheat Sheet for Javascript Testing with Jasmine"}]},{"@type":"WebSite","@id":"https:\/\/thekua.com\/atwork\/#website","url":"https:\/\/thekua.com\/atwork\/","name":"patkua@work","description":"The intersection of technology and leadership","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thekua.com\/atwork\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae","name":"Patrick","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","caption":"Patrick"},"sameAs":["http:\/\/thekua.com\/atwork"],"url":"https:\/\/thekua.com\/atwork\/author\/patrick\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/comments?post=1740"}],"version-history":[{"count":7,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1740\/revisions"}],"predecessor-version":[{"id":3470,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1740\/revisions\/3470"}],"wp:attachment":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/media?parent=1740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/categories?post=1740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/tags?post=1740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}