{"id":10,"date":"2013-03-22T17:25:56","date_gmt":"2013-03-22T17:25:56","guid":{"rendered":"http:\/\/www.yeetrack.com\/post\/2013-03-21\/jasmine%E6%B5%8B%E8%AF%95%E6%A1%86%E6%9E%B6%E7%AE%80%E4%BB%8B"},"modified":"2013-09-27T22:36:46","modified_gmt":"2013-09-27T14:36:46","slug":"jasmine%e6%b5%8b%e8%af%95%e6%a1%86%e6%9e%b6%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=10","title":{"rendered":"jasmine\u6d4b\u8bd5\u6846\u67b6\u7b80\u4ecb"},"content":{"rendered":"<p><strong>jasmine<\/strong>\u662f\u4e00\u79cdjavascript\u6d4b\u8bd5\u6846\u67b6\uff0c\u5b83\u65e2\u53ef\u4ee5\u5728html\u6587\u4ef6\u4e2d\u8fd0\u884c\uff0c\u4e5f\u53ef\u4ee5\u548cjsTestDriver\u6574\u5408\uff0c\u5728jsTestDriver\u4e2d\u8fd0\u884c\u3002<\/p>\n<h3>jasmine\u7684\u7b80\u5355\u8bed\u6cd5<\/h3>\n<p>\u4e00\u4e2a\u57fa\u672c\u7684jasmine\u6d4b\u8bd5\u7528\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre config=\"brush:js;toolbar:false;\">\ndescribe(&quot;A suite&quot;, function() {  \n        it(&quot;contains spec with an expectation&quot;, function() {  \n        expect(true).toBe(true);  \n        });  \n    }); \n<\/pre>\n<p><!--more--><\/p>\n<h4>describe\u65b9\u6cd5<\/h4>\n<p>describe\u65b9\u6cd5\u6807\u5fd7\u7740\u4e00\u4e2a\u6d4b\u8bd5\u96c6(test suite)\u7684\u5f00\u59cb\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u5b57\u7b26\u4e32String\uff0c\u4e00\u4e2a\u65b9\u6cd5function\uff1b\u5b57\u7b26\u4e32\u7528\u6765\u63cf\u8ff0\u6211\u4eec\u8fd9\u4e2atest suite\uff0cfunction\u91cc\u7684\u4e1c\u897f\u5c31\u662f\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5b83\u4eec\u5c31\u662fsuite\u3002<\/p>\n<h4>it\u65b9\u6cd5<\/h4>\n<p>jasmine\u4e2d\u7528\u65b9\u6cd5it\u6765\u5f00\u59cbspecs\uff08\u6211\u7406\u89e3\u6210\u6d4b\u8bd5\u70b9\uff0c\u4e00\u4e2a\u6d4b\u8bd5suite\u91cc\u53ef\u4ee5\u6709\u5f88\u591aspec\uff09\u3002it\u65b9\u6cd5\u548cdescribe\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u540c\u6837\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2aString\uff0c\u4e00\u4e2afunction\uff1bString\u7528\u6765\u63cf\u8ff0\u6d4b\u8bd5\u70b9\uff08spec\uff09\uff0cfunction\u662f\u5177\u4f53\u7684\u6d4b\u8bd5\u4ee3\u7801\u3002\u4e00\u4e2a\u6d4b\u8bd5\u70b9(spec)\u53ef\u4ee5\u5305\u542b\u591a\u4e2aexpections\uff08\u5176\u5b9e\u662f\u65ad\u8a00\u7684\u610f\u601d\uff09\u3002<\/p>\n<h4>expectations<\/h4>\n<p>\u65ad\u8a00\u53ef\u4ee5\u8fd4\u56de\u4e3atrue\u6216\u8005false\u3002\u5168\u90e8\u7684\u65ad\u8a00\u8fd4\u56detrue\u8fd9\u4e2a\u6d4b\u8bd5\u70b9\u5c31\u901a\u8fc7\uff0c\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u65ad\u8a00\u8fd4\u56defalse\u8fd9\u4e2a\u6d4b\u8bd5\u70b9\u5c31\u4e0d\u901a\u8fc7\u3002<br \/> describe\u548cit\u90fd\u662f\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e9b\u53d8\u91cf\uff0c\u5728describe\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5728it\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<pre config=\"brush:js;toolbar:false;\">\ndescribe(&quot;A suite is just a function&quot;, function() {\n    var a;\n\n    it(&quot;and so is a spec&quot;, function() {\n        a = true;\n\n        expect(a).toBe(true);\n    });\n    });   \n<\/pre>\n<h4>Matchers<\/h4>\n<p>\u4e00\u4e2a\u4e00\u4e2a\u7684\u6d4b\u8bd5\u70b9\u4eec\u7531expect\u5f00\u5934\uff0c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u7684\u53d8\u91cf\uff0c\u5982\u4e0a\u9762\u7684a\uff0c\u7136\u540e\u8ddf\u7740\u4e00\u4e2aMatcher\u65b9\u6cd5\uff08\u6211\u7406\u89e3\u6210\u6821\u9a8c\u89c4\u5219\uff09\uff0cMatcher\u65b9\u6cd5\u5e26\u7740\u4e00\u4e2a\u671f\u671b\u503c\uff0c\u5982\u4e0a\u9762\u7684true\u3002Matchers\u65b9\u6cd5\u8fd4\u56detrue\u6216\u8005false\uff0c\u5b83\u51b3\u5b9a\u7740\u6d4b\u8bd5\u70b9\uff08spec\uff09\u662f\u5426\u901a\u8fc7\u3002\u6240\u6709\u7684Matchers\u65b9\u6cd5\u90fd\u80fd\u5728mathcer\u524d\u9762\u52a0\u4e0anot\u6765\u8fdb\u884c\u5426\u5b9a\u65ad\u8a00\uff0c\u5982`expect(a).not.toBe(true);<\/p>\n<p>jasmine\u4e2d\u6709\u5f88\u591aMatchers\u65b9\u6cd5\u4f9b\u6211\u4eec\u4f7f\u7528\uff0c\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684Matchers\u65b9\u6cd5\u3002<\/p>\n<pre config=\"brush:js;toolbar:false;\">\ndescribe(&quot;Included matchers:&quot;, function() {\n\n        it(&quot;The 'toBe' matcher compares with ===&quot;, function() {\n            var a = 12;\n            var b = a;\n\n            expect(a).toBe(b);\n            expect(a).not.toBe(null);\n        });  \n        \/\/\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6bd4\u8f83a\u3001b\u662f\u5426\u76f8\u7b49\uff1b\u9a8c\u8bc1a\u662f\u5426\u4e0d\u662f\u7a7a\u3002 \n\n        it(&quot;should work for objects&quot;, function() {\n            var foo = {\n                a: 12,\n                b: 34\n            };\n            var bar = {\n                a: 12,\n                b: 34\n            };\n            expect(foo).toEqual(bar);\n        });\n        \/\/\u4e0a\u9762\u7684\u4f8b\u5b50\u6bd4\u8f83\u4e86\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\n    });\n\n    it(&quot;The 'toMatch' matcher is for regular expressions&quot;, function() {\n        var message = 'foo bar baz';\n\n        expect(message).toMatch(\/bar\/);\n        expect(message).toMatch('bar');\n        expect(message).not.toMatch(\/quux\/);\n    });\n    \/\/\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\n    it(&quot;The 'toBeDefined' matcher compares against `undefined`&quot;, function() {\n        var a = {\n            foo: 'foo'\n        };\n\n        expect(a.foo).toBeDefined();\n        expect(a.bar).not.toBeDefined();\n    });\n    \/\/\u9a8c\u8bc1\u53d8\u91cf\u662f\u5426\u88ab\u5b9a\u4e49  \n\n    it(&quot;The 'toBeNull' matcher compares against null&quot;, function() {\n        var a = null;\n        var foo = 'foo';\n\n        expect(null).toBeNull();\n        expect(a).toBeNull();\n        expect(foo).not.toBeNull();\n    });\n    \/\/\u9a8c\u8bc1\u662f\u5426\u4e3a\u7a7a\n\n    it(&quot;The 'toBeTruthy' matcher is for boolean casting testing&quot;, function() {\n        var a, foo = 'foo';\n\n        expect(foo).toBeTruthy();\n        expect(a).not.toBeTruthy();\n    });\n\n    it(&quot;The 'toBeFalsy' matcher is for boolean casting testing&quot;, function() {\n        var a, foo = 'foo';\n\n        expect(a).toBeFalsy();\n        expect(foo).not.toBeFalsy();\n    });\n    \/\/\u53d8\u91cf\u662f\u5426\u80fd\u591f\u8f6c\u5316\u6210boolean\u53d8\u91cf\uff1f \u4e0d\u592a\u786e\u5b9a\n\n    it(&quot;The 'toContain' matcher is for finding an item in an Array&quot;, function() {\n        var a = ['foo', 'bar', 'baz'];\n\n        expect(a).toContain('bar');\n        expect(a).not.toContain('quux');\n    });\n    \/\/\u662f\u5426\u5305\u542b\n    it(&quot;The 'toBeLessThan' matcher is for mathematical comparisons&quot;, function() {\n        var pi = 3.1415926, e = 2.78;\n\n        expect(e).toBeLessThan(pi);\n        expect(pi).not.toBeLessThan(e);\n    });\n\n    it(&quot;The 'toBeGreaterThan' is for mathematical comparisons&quot;, function() {\n        var pi = 3.1415926, e = 2.78;\n\n        expect(pi).toBeGreaterThan(e);\n        expect(e).not.toBeGreaterThan(pi);\n    });\n    \/\/\u6570\u5b66\u5927\u5c0f\u7684\u6bd4\u8f83\n\n    it(&quot;The 'toBeCloseTo' matcher is for precision math comparison&quot;, function() {\n    var pi = 3.1415926, e = 2.78;\n\n    expect(pi).not.toBeCloseTo(e, 2);\n    expect(pi).toBeCloseTo(e, 0);\n    });\n    \/\/\u4e24\u4e2a\u6570\u503c\u662f\u5426\u63a5\u8fd1\uff0c\u8fd9\u91cc\u63a5\u8fd1\u7684\u610f\u601d\u662f\u5c06pi\u548ce\u4fdd\u7559\u4e00\u5b9a\u5c0f\u6570\u4f4d\u6570\u540e\uff0c\u662f\u5426\u76f8\u7b49\u3002\uff08\u4e00\u5b9a\u5c0f\u6570\u4f4d\u6570\uff1a\u9ed8\u8ba4\u4e3a2\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a\uff09\n\n    it(&quot;The 'toThrow' matcher is for testing if a function throws an exception&quot;, function() {\n        var foo = function() {\n        return 1 + 2;\n        };\n        var bar = function() {\n            return a + 1;\n        };\n\n        expect(foo).not.toThrow();\n        expect(bar).toThrow();\n        });\n    }); \n    \/\/\u6d4b\u8bd5\u4e00\u4e2a\u65b9\u6cd5\u662f\u5426\u629b\u51fa\u5f02\u5e38  \n<\/pre>\n<h4>Setup\u548cTeardown\u65b9\u6cd5<\/h4>\n<p>\u4e3a\u4e86\u4ee3\u7801\u7b80\u6d01\uff0c\u51cf\u5c11\u91cd\u590d\u6027\u7684\u5de5\u4f5c\uff0cjasmine\u63d0\u4f9b<code>beforeEach<\/code>\u548c<code>afterEach<\/code>\u65b9\u6cd5\u3002<code>beforeEach<\/code>\u4f1a\u5728\u6bcf\u4e2aspec\u4e4b\u524d\u6267\u884c\uff0c<code>after<\/code>\u4f1a\u5728\u6bcf\u4e2aspec\u4e4b\u540e\u6267\u884c\uff0c\u7c7b\u4f3c\u4e8eselenium\u4e2d\u7684<code>beforeMethod<\/code>\u548c<code>afterMethod<\/code>\u65b9\u6cd5\u3002<\/p>\n<pre config=\"brush:js;toolbar:false;\">\n describe(&quot;A spec (with setup and tear-down)&quot;, function() {\n        var foo;\n\n        beforeEach(function() {\n            foo = 1;\n        });\n\n        afterEach(function() {\n            foo = 0;\n        });\n\n        it(&quot;is just a function, so it can contain any code&quot;, function() {\n            expect(foo).toEqual(1);\n        });\n\n        it(&quot;can have more than one expectation&quot;, function() {\n            expect(foo).toEqual(1);\n            expect(true).toEqual(true);\n        });\n    });  \n<\/pre>\n<p>\u53e6\u5916describe\u548cit\u4f5c\u4e3a\u65b9\u6cd5\u662f\u53ef\u4ee5\u5d4c\u5957\u7684\uff0c\u4e5f\u5c31\u662fdescribe\u4e2d\u53ef\u4ee5\u51fa\u73b0\u5b50describe\u548cit\u3002<\/p>\n<h4>\u7981\u7528\u67d0\u4e9bspec\u548csuites<\/h4>\n<p>\u5728\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7981\u7528\u4e00\u4e9bsuites\u548cspec\uff0c\u65b9\u6cd5\u662f\u4f7f\u7528xdescribe\u548cxit\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u6d4b\u8bd5\u7684\u65b9\u6cd5\u4f1a\u88ab\u5ffd\u7565\uff0c\u4e0d\u8ba1\u5165\u7edf\u8ba1\u7ed3\u679c\u3002<\/p>\n<h4>The Runner and Reporter<\/h4>\n<p>Jasmine\u662f\u7528javascript\u5b9e\u73b0\u7684\uff0c\u6240\u4ee5\u5b83\u4e5f\u5fc5\u987b\u5728javascript\u7684\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u6700\u7b80\u5355\u7684\u73af\u5883\u4e5f\u5c31\u662f\u4e00\u4e2aweb\u9875\u9762\u3002\u6240\u6709\u7684spec\u90fd\u53ef\u4ee5\u5728\u8fd9\u4e2a\u9875\u9762\u4e2d\u8fd0\u884c\uff0c\u8fd9\u4e2a\u9875\u9762\u5c31\u53eb\u505aRunner\u3002<\/p>\n<p>Jasmine\u901a\u8fc7\u4e0b\u9762\u7684js\u4ee3\u7801\u6765\u5c55\u73b0spec\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre config=\"brush:js;toolbar:false;\">\nvar htmlReporter = new jasmine.HtmlReporter(); \/\/\u521b\u5efa\u4e00\u4e2aHTMLReporter\n    jasmineEnv.addReporter(htmlReporter);  \n\n    jasmineEnv.specFilter = function(spec) {  \/\/\u4e00\u4e2a\u8fc7\u6ee4\u5668\uff0c\u5141\u8bb8\u6211\u4eec\u70b9\u51fb\u5355\u4e2a\u7684suites\uff0c\u5355\u72ec\u8fd0\u884c\n    return htmlReporter.specFilter(spec);\n    };  \n\n\n    var currentWindowOnload = window.onload;   \/\/\u9875\u9762\u52a0\u8f7d\u5b8c\u6bd5\u540e\uff0c\u6267\u884c\u6240\u6709\u7684test\u3002\n    window.onload = function() {\n        if (currentWindowOnload) {\n            currentWindowOnload();\n        }\n\n        document.querySelector('.version').innerHTML = jasmineEnv.versionString();\n        execJasmine();\n    };\n\n    function execJasmine() {\n            jasmineEnv.execute();\n    }\n    })();   \n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jasmine\u662f\u4e00\u79cdjavascript\u6d4b\u8bd5\u6846\u67b6\uff0c\u5b83\u65e2\u53ef\u4ee5\u5728html\u6587\u4ef6\u4e2d\u8fd0\u884c\uff0c\u4e5f\u53ef\u4ee5\u548cjsTestDriver\u6574\u5408\uff0c\u5728jsTestDriver\u4e2d\u8fd0\u884c\u3002 jasmine\u7684\u7b80\u5355\u8bed\u6cd5 \u4e00\u4e2a\u57fa\u672c\u7684jasmi&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[9,7,5],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-coding","tag-javascript","tag-7","tag-5"],"views":3542,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":186,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions\/186"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}