_examples.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**
  2. * Created with JetBrains PhpStorm.
  3. * User: dongyancen
  4. * Date: 13-10-12
  5. * Time: 下午1:18
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. /*
  9. * matchFunctions
  10. * */
  11. describe("matchFunctions examples", function () {
  12. //toBe 相当于===,处理简单字面值和变量
  13. it("toBe相当于===", function () {
  14. var a = 12;
  15. var b = a;
  16. expect(a).toBe(b);
  17. expect(a).not.toBe(null);
  18. expect(false == 0).toBe(true);
  19. });
  20. it("toBe不能当==用", function () {
  21. expect(false).toBe(0);
  22. });
  23. //toEqual 处理简单字面值和变量,而且可以处理对象,数组
  24. it("toEqual可以处理字面值,变量和对象", function () {
  25. var a = 12;
  26. expect(a).toEqual(12);
  27. var foo = {key: "key"};
  28. var bar = {key: "key"};
  29. expect(foo).toEqual(bar);
  30. var arr1 = [];
  31. arr1["p1"] = "string1";
  32. var arr2 = [];
  33. arr2["p1"] = "string1";
  34. var obj = {};
  35. obj["p1"] = "string1";
  36. expect(arr1).toEqual(arr2);
  37. expect(arr1).toEqual(obj);
  38. });
  39. //toMatch 按正则式检索。
  40. it("toMatch匹配正则式", function () {
  41. var message = "foo bar baz";
  42. expect(message).toMatch(/bar/);
  43. expect(message).toMatch("bar");
  44. expect(message).not.toMatch(/quux/);
  45. expect(message).toMatch(/^f/);
  46. expect(message).not.toMatch(/f$/);
  47. });
  48. //toBeDefined 是否已声明且赋值
  49. it("toBeDefined检测变量非undefined", function () {
  50. var a = { key: "key"};
  51. expect(a.key).toBeDefined();
  52. expect(a.foo).not.toBeDefined();
  53. //expect(c).not.toBeDefined(); //未声明出错
  54. var b;
  55. expect(b).not.toBeDefined();
  56. //对象.未声明属性.not.toBeDefined(); 通过
  57. //未声明变量.not.toBeDefined(); 报错
  58. });
  59. //toBeLessThan 数值比较,小于
  60. //
  61. //toBeGreaterThan 数值比较,大于
  62. //
  63. //toBeCloseTo 数值比较时定义精度,先四舍五入后再比较
  64. it("toBeCloseTo数值比较,指定精度,先四舍五入再比较", function () {
  65. var pi = 3.1415926, e = 2.78;
  66. expect(pi).toBeCloseTo(e, 0);
  67. expect(pi).not.toBeCloseTo(e, 0.1);
  68. });
  69. //toThrow 检验一个函数是否会抛出一个错误
  70. it("toThrow检验一个函数是否会抛出一个错误", function () {
  71. var foo = function () {
  72. return 1 + 2;
  73. };
  74. var bar = function () {
  75. return a + 1;
  76. };
  77. expect(foo).not.toThrow();
  78. expect(bar).toThrow();
  79. });
  80. });
  81. /*
  82. * Spy
  83. * */
  84. describe("Spy examples", function () {
  85. //Spy 存储函数的被调用情况和参数(函数监视器,记录被调用情况,但函数并不真执行)
  86. describe("对spy函数的测试", function () {
  87. var foo, bar = null;
  88. beforeEach(function () {
  89. foo = {
  90. setBar: function (value) {
  91. bar = value;
  92. }
  93. };
  94. spyOn(foo, 'setBar'); //foo为spy函数
  95. foo.setBar(123);
  96. foo.setBar(456, 'another param');
  97. });
  98. it("测试foo函数是否被调用过", function () {
  99. expect(foo.setBar).toHaveBeenCalled();
  100. });
  101. it("测试foo函数被调用的次数", function () {
  102. expect(foo.setBar.calls.length).toEqual(2);
  103. });
  104. it("测试foo函数被调用时传入的参数", function () {
  105. expect(foo.setBar).toHaveBeenCalledWith(123);
  106. expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
  107. });
  108. it("上一次被调用的参数", function () {
  109. expect(foo.setBar.mostRecentCall.args[0]).toEqual(456);
  110. });
  111. it("所有被调用的情况存在一个数组里", function () {
  112. expect(foo.setBar.calls[0].args[0]).toEqual(123);
  113. });
  114. it("函数并未真的执行", function () {
  115. expect(bar).toBeNull();
  116. });
  117. });
  118. //Spy addCallThrough 函数监视器,但函数真的执行
  119. describe("对spy函数的测试,函数真的执行", function () {
  120. var foo, bar, fetchedBar;
  121. beforeEach(function () {
  122. foo = {
  123. setBar: function (value) {
  124. bar = value;
  125. },
  126. getBar: function () {
  127. return bar;
  128. }
  129. };
  130. //spyOn(foo, "setBar"); //如果加上这句,setBar不真的执行,后两个spec不通过
  131. spyOn(foo, 'getBar').andCallThrough();
  132. foo.setBar(123);
  133. fetchedBar = foo.getBar();
  134. });
  135. it("测试foo中getBar函数是否被调用过", function () {
  136. expect(foo.getBar).toHaveBeenCalled();
  137. });
  138. it("foo中setBar函数真的执行了", function () {
  139. expect(bar).toEqual(123);
  140. });
  141. it("foo中getBar函数真的执行了", function () {
  142. expect(fetchedBar).toEqual(123);
  143. });
  144. });
  145. //Spy andReturn 函数监视器,函数不真的执行。指定监视的函数的返回值
  146. describe("A spy, when faking a return value", function () {
  147. var foo, bar, fetchedBar;
  148. beforeEach(function () {
  149. foo = {
  150. setBar: function (value) {
  151. bar = value;
  152. },
  153. getBar: function () {
  154. return bar;
  155. }
  156. };
  157. spyOn(foo, 'getBar').andReturn(745); //指定getBar函数返回745
  158. foo.setBar(123);
  159. fetchedBar = foo.getBar();
  160. });
  161. it("测试foo中getBar函数是否被调用过", function () {
  162. expect(foo.getBar).toHaveBeenCalled();
  163. });
  164. it("不影响未被监视的其它函数", function () {
  165. expect(bar).toEqual(123);
  166. });
  167. it("指定的返回值745", function () {
  168. expect(fetchedBar).toEqual(745);
  169. });
  170. });
  171. //Spy addCallFake 替代被监视的函数,原函数不执行
  172. describe("替代被监视的函数,原函数不执行", function () {
  173. var foo, bar, fetchedBar;
  174. beforeEach(function () {
  175. foo = {
  176. setBar: function (value) {
  177. bar = value;
  178. },
  179. getBar: function () {
  180. alert("frostbelt");
  181. return bar;
  182. }
  183. };
  184. spyOn(foo, 'getBar').andCallFake(function () {
  185. return 1001;
  186. });
  187. foo.setBar(123);
  188. fetchedBar = foo.getBar();
  189. });
  190. it("测试foo中getBar函数是否被调用过", function () {
  191. expect(foo.getBar).toHaveBeenCalled();
  192. });
  193. it("不影响未被监视的其它函数", function () {
  194. expect(bar).toEqual(123);
  195. });
  196. it("getBar被addCallFake指定的匿名函数代替,getBar不执行", function () {
  197. expect(fetchedBar).toEqual(1001);
  198. });
  199. });
  200. //自己create一个被监视函数
  201. //
  202. //jasmine.createSpy(functionId)
  203. describe("自己造一个被监视函数", function () {
  204. var whatAmI;
  205. beforeEach(function () {
  206. whatAmI = jasmine.createSpy('whatAmI');
  207. whatAmI("I", "am", "a", "spy");
  208. });
  209. it("有个id,是createSpy的传入函数,用于报错", function () {
  210. expect(whatAmI.identity).toEqual('whatAmI')
  211. });
  212. it("是否被调用", function () {
  213. expect(whatAmI).toHaveBeenCalled();
  214. });
  215. it("被调用的次数", function () {
  216. expect(whatAmI.calls.length).toEqual(1);
  217. });
  218. it("被调用的参数", function () {
  219. expect(whatAmI).toHaveBeenCalledWith("I", "am", "a", "spy");
  220. });
  221. it("最近一次被调用", function () {
  222. expect(whatAmI.mostRecentCall.args[0]).toEqual("I");
  223. });
  224. });
  225. //有时需要监视一个对象的很多方法,用createSpyObj添加方法数组
  226. //jasmine.createSpyObj(obj, methodArray)
  227. describe("有时需要监视一个对象的很多个方法,用createSpyObj添加数组", function () {
  228. var tape;
  229. beforeEach(function () {
  230. tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);
  231. tape.play();
  232. tape.pause();
  233. tape.rewind(0);
  234. });
  235. it("tape对象的这四个方法已被定义", function () {
  236. expect(tape.play).toBeDefined();
  237. expect(tape.pause).toBeDefined();
  238. expect(tape.stop).toBeDefined();
  239. expect(tape.rewind).toBeDefined();
  240. });
  241. it("四个方法是否被调用", function () {
  242. expect(tape.play).toHaveBeenCalled();
  243. expect(tape.pause).toHaveBeenCalled();
  244. expect(tape.rewind).toHaveBeenCalled();
  245. expect(tape.stop).not.toHaveBeenCalled();
  246. });
  247. it("被调用时传入的参数", function () {
  248. expect(tape.rewind).toHaveBeenCalledWith(0);
  249. });
  250. });
  251. });
  252. /*
  253. * instanceof
  254. * */
  255. describe("instanceof examples", function() {
  256. //jasmine.any 类型判断。instanceof
  257. it("相当于instanceof", function() {
  258. expect({}).toEqual(jasmine.any(Object));
  259. expect(12).toEqual(jasmine.any(Number));
  260. });
  261. it("也可以用于spy", function() {
  262. var foo = jasmine.createSpy('foo');
  263. foo(12, function() {
  264. return true
  265. });
  266. expect(foo).toHaveBeenCalledWith(jasmine.any(Number), jasmine.any(Function));
  267. //foo被调用时的参数 类型判断
  268. });
  269. });
  270. /*
  271. * jasmine.Clock.useMock() jasmine自己控制时间,实现异步调试,减少等待
  272. * jasmine.Clock.tick(n:uint) 向前n毫秒
  273. * */
  274. describe("useMock + tick examples", function() {
  275. var timerCallback;
  276. beforeEach(function() {
  277. timerCallback = jasmine.createSpy('timerCallback');
  278. jasmine.Clock.useMock();
  279. });
  280. it("setTimeout", function() {
  281. setTimeout(function() {
  282. timerCallback();
  283. }, 100);
  284. expect(timerCallback).not.toHaveBeenCalled();
  285. jasmine.Clock.tick(101);
  286. expect(timerCallback).toHaveBeenCalled();
  287. });
  288. it("setInterval", function() {
  289. setInterval(function() {
  290. timerCallback();
  291. }, 100);
  292. expect(timerCallback).not.toHaveBeenCalled();
  293. jasmine.Clock.tick(101);
  294. expect(timerCallback.callCount).toEqual(1);
  295. jasmine.Clock.tick(50);
  296. expect(timerCallback.callCount).toEqual(1);
  297. jasmine.Clock.tick(50);
  298. expect(timerCallback.callCount).toEqual(2);
  299. });
  300. //注:在这种环境下setTimeout和setInterval的callback为同步的,系统时间不再影响执行
  301. });
  302. /*
  303. *
  304. * runs(function) waitsFor(function, message, millisec) Jasmine异步调试
  305. * */
  306. describe("runs+waitsFor examples", function() {
  307. var value, flag;
  308. it("这里写你测试需要异步的代码", function() {
  309. runs(function() {
  310. flag = false;
  311. value = 0;
  312. setTimeout(function() {
  313. flag = true;
  314. }, 500);
  315. });
  316. waitsFor(function() { //如果750 毫秒之内返回的flag为true,则执行runs方法,否则报错,返回并且显示第二个参数的内容
  317. value++;
  318. return flag;
  319. }, "说明", 750);
  320. runs(function() {
  321. expect(value).toBeGreaterThan(0);
  322. });
  323. });
  324. });
  325. describe(" example2", function() {
  326. //这里使用的是SpecHelper里面自定义的断言
  327. it('helper',function(){
  328. expect(true).toBe(true);
  329. });
  330. });