test.mock.mock.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* global require, chai, describe, before, it */
  2. // 数据占位符定义(Data Placeholder Definition,DPD)
  3. var expect = chai.expect
  4. var Mock, $, _
  5. describe('Mock.mock', function() {
  6. before(function(done) {
  7. require(['mock', 'underscore', 'jquery'], function() {
  8. Mock = arguments[0]
  9. _ = arguments[1]
  10. $ = arguments[2]
  11. expect(Mock).to.not.equal(undefined)
  12. expect(_).to.not.equal(undefined)
  13. expect($).to.not.equal(undefined)
  14. done()
  15. })
  16. })
  17. describe('Mock.mock( String )', function() {
  18. it('@EMAIL', function() {
  19. var data = Mock.mock(this.test.title)
  20. expect(data).to.not.equal(this.test.title)
  21. this.test.title += ' => ' + data
  22. })
  23. })
  24. describe('Mock.mock( {} )', function() {
  25. it('', function() {
  26. var tpl = {
  27. 'list|1-10': [{
  28. 'id|+1': 1,
  29. 'email': '@EMAIL'
  30. }]
  31. }
  32. var data = Mock.mock(tpl)
  33. this.test.title = JSON.stringify(tpl /*, null, 4*/ ) + ' => ' + JSON.stringify(data /*, null, 4*/ )
  34. expect(data).to.have.property('list')
  35. .that.be.an('array').with.length.within(1, 10)
  36. _.each(data.list, function(item, index, list) {
  37. if (index > 0) expect(item.id).to.equal(list[index - 1].id + 1)
  38. })
  39. })
  40. })
  41. describe('Mock.mock( function() )', function() {
  42. it('', function() {
  43. var fn = function() {
  44. return Mock.mock({
  45. 'list|1-10': [{
  46. 'id|+1': 1,
  47. 'email': '@EMAIL'
  48. }]
  49. })
  50. }
  51. var data = Mock.mock(fn)
  52. this.test.title = fn.toString() + ' => ' + JSON.stringify(data /*, null, 4*/ )
  53. expect(data).to.have.property('list')
  54. .that.be.an('array').with.length.within(1, 10)
  55. _.each(data.list, function(item, index, list) {
  56. if (index > 0) expect(item.id).to.equal(list[index - 1].id + 1)
  57. })
  58. })
  59. })
  60. })