Guest User

Untitled

a guest
Jan 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. local moduleFoo={}
  2. local moduleBaz= require("moduleBaz")
  3.  
  4. moduleFoo.doSomething = function (arg)
  5.  
  6. if moduleBaz.bar.neatMethod(arg) then
  7. --does something interesting
  8. end
  9.  
  10. end
  11.  
  12. return moduleFoo
  13.  
  14. local moduleBaz={}
  15. moduleBaz.bar= {}
  16.  
  17. moduleBaz.bar.neatMethod=function(arg)
  18. --does something neat
  19. end
  20. return moduleBaz
  21.  
  22. package.loaded.moduleBaz= nil
  23. local moduleBaz = {}
  24. moduleBaz.bar = {}
  25. moduleBaz.bar.neatMethod= function(guid) return true end
  26.  
  27. package.preload['moduleBaz'] = function ()
  28. return moduleBaz
  29. end
  30.  
  31. local foo= require("moduleFoo")
  32. foo.doSomething('asdasdasda')--real moduleBaz is called, not my mock!
  33.  
  34. return moduleBaz
  35.  
  36. package.loaded.moduleBaz = {
  37. bar = {
  38. neatmethod = function(arg)
  39. -- your mock code here
  40. end,
  41. }
  42. }
  43.  
  44. local moduleBaz={}
  45. moduleBaz.bar= {}
  46. moduleBaz.bar.neatMethod=function(arg)
  47. print "baz"
  48. return true
  49. end
  50. return moduleBaz
  51.  
  52. local moduleFoo={}
  53. local moduleBaz= require("moduleBaz")
  54. moduleFoo.doSomething = function (arg)
  55. if moduleBaz.bar.neatMethod(arg) then
  56. print "foo"
  57. end
  58. end
  59. return moduleFoo
  60.  
  61. package.loaded.moduleBaz= nil
  62. local moduleBaz = {}
  63. moduleBaz.bar = {}
  64. moduleBaz.bar.neatMethod= function(guid) print "mock" return true end
  65.  
  66. package.preload['moduleBaz'] = function ()
  67. return moduleBaz
  68. end
  69.  
  70. local foo= require("moduleFoo")
  71. foo.doSomething('asdasdasda')--real moduleBaz is called, not my mock!
Add Comment
Please, Sign In to add comment