mock_test.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. package mock
  2. import (
  3. "errors"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "testing"
  7. "time"
  8. )
  9. /*
  10. Test objects
  11. */
  12. // ExampleInterface represents an example interface.
  13. type ExampleInterface interface {
  14. TheExampleMethod(a, b, c int) (int, error)
  15. }
  16. // TestExampleImplementation is a test implementation of ExampleInterface
  17. type TestExampleImplementation struct {
  18. Mock
  19. }
  20. func (i *TestExampleImplementation) TheExampleMethod(a, b, c int) (int, error) {
  21. args := i.Called(a, b, c)
  22. return args.Int(0), errors.New("Whoops")
  23. }
  24. func (i *TestExampleImplementation) TheExampleMethod2(yesorno bool) {
  25. i.Called(yesorno)
  26. }
  27. type ExampleType struct {
  28. ran bool
  29. }
  30. func (i *TestExampleImplementation) TheExampleMethod3(et *ExampleType) error {
  31. args := i.Called(et)
  32. return args.Error(0)
  33. }
  34. func (i *TestExampleImplementation) TheExampleMethodFunc(fn func(string) error) error {
  35. args := i.Called(fn)
  36. return args.Error(0)
  37. }
  38. func (i *TestExampleImplementation) TheExampleMethodVariadic(a ...int) error {
  39. args := i.Called(a)
  40. return args.Error(0)
  41. }
  42. func (i *TestExampleImplementation) TheExampleMethodVariadicInterface(a ...interface{}) error {
  43. args := i.Called(a)
  44. return args.Error(0)
  45. }
  46. type ExampleFuncType func(string) error
  47. func (i *TestExampleImplementation) TheExampleMethodFuncType(fn ExampleFuncType) error {
  48. args := i.Called(fn)
  49. return args.Error(0)
  50. }
  51. /*
  52. Mock
  53. */
  54. func Test_Mock_TestData(t *testing.T) {
  55. var mockedService = new(TestExampleImplementation)
  56. if assert.NotNil(t, mockedService.TestData()) {
  57. mockedService.TestData().Set("something", 123)
  58. assert.Equal(t, 123, mockedService.TestData().Get("something").Data())
  59. }
  60. }
  61. func Test_Mock_On(t *testing.T) {
  62. // make a test impl object
  63. var mockedService = new(TestExampleImplementation)
  64. c := mockedService.On("TheExampleMethod")
  65. assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  66. assert.Equal(t, "TheExampleMethod", c.Method)
  67. }
  68. func Test_Mock_Chained_On(t *testing.T) {
  69. // make a test impl object
  70. var mockedService = new(TestExampleImplementation)
  71. mockedService.
  72. On("TheExampleMethod", 1, 2, 3).
  73. Return(0).
  74. On("TheExampleMethod3", AnythingOfType("*mock.ExampleType")).
  75. Return(nil)
  76. expectedCalls := []*Call{
  77. &Call{
  78. Parent: &mockedService.Mock,
  79. Method: "TheExampleMethod",
  80. Arguments: []interface{}{1, 2, 3},
  81. ReturnArguments: []interface{}{0},
  82. },
  83. &Call{
  84. Parent: &mockedService.Mock,
  85. Method: "TheExampleMethod3",
  86. Arguments: []interface{}{AnythingOfType("*mock.ExampleType")},
  87. ReturnArguments: []interface{}{nil},
  88. },
  89. }
  90. assert.Equal(t, expectedCalls, mockedService.ExpectedCalls)
  91. }
  92. func Test_Mock_On_WithArgs(t *testing.T) {
  93. // make a test impl object
  94. var mockedService = new(TestExampleImplementation)
  95. c := mockedService.On("TheExampleMethod", 1, 2, 3, 4)
  96. assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  97. assert.Equal(t, "TheExampleMethod", c.Method)
  98. assert.Equal(t, Arguments{1, 2, 3, 4}, c.Arguments)
  99. }
  100. func Test_Mock_On_WithFuncArg(t *testing.T) {
  101. // make a test impl object
  102. var mockedService = new(TestExampleImplementation)
  103. c := mockedService.
  104. On("TheExampleMethodFunc", AnythingOfType("func(string) error")).
  105. Return(nil)
  106. assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  107. assert.Equal(t, "TheExampleMethodFunc", c.Method)
  108. assert.Equal(t, 1, len(c.Arguments))
  109. assert.Equal(t, AnythingOfType("func(string) error"), c.Arguments[0])
  110. fn := func(string) error { return nil }
  111. assert.NotPanics(t, func() {
  112. mockedService.TheExampleMethodFunc(fn)
  113. })
  114. }
  115. func Test_Mock_On_WithIntArgMatcher(t *testing.T) {
  116. var mockedService TestExampleImplementation
  117. mockedService.On("TheExampleMethod",
  118. MatchedBy(func(a int) bool {
  119. return a == 1
  120. }), MatchedBy(func(b int) bool {
  121. return b == 2
  122. }), MatchedBy(func(c int) bool {
  123. return c == 3
  124. })).Return(0, nil)
  125. assert.Panics(t, func() {
  126. mockedService.TheExampleMethod(1, 2, 4)
  127. })
  128. assert.Panics(t, func() {
  129. mockedService.TheExampleMethod(2, 2, 3)
  130. })
  131. assert.NotPanics(t, func() {
  132. mockedService.TheExampleMethod(1, 2, 3)
  133. })
  134. }
  135. func Test_Mock_On_WithPtrArgMatcher(t *testing.T) {
  136. var mockedService TestExampleImplementation
  137. mockedService.On("TheExampleMethod3",
  138. MatchedBy(func(a *ExampleType) bool { return a.ran == true }),
  139. ).Return(nil)
  140. mockedService.On("TheExampleMethod3",
  141. MatchedBy(func(a *ExampleType) bool { return a.ran == false }),
  142. ).Return(errors.New("error"))
  143. assert.Equal(t, mockedService.TheExampleMethod3(&ExampleType{true}), nil)
  144. assert.EqualError(t, mockedService.TheExampleMethod3(&ExampleType{false}), "error")
  145. }
  146. func Test_Mock_On_WithFuncArgMatcher(t *testing.T) {
  147. var mockedService TestExampleImplementation
  148. fixture1, fixture2 := errors.New("fixture1"), errors.New("fixture2")
  149. mockedService.On("TheExampleMethodFunc",
  150. MatchedBy(func(a func(string) error) bool { return a("string") == fixture1 }),
  151. ).Return(errors.New("fixture1"))
  152. mockedService.On("TheExampleMethodFunc",
  153. MatchedBy(func(a func(string) error) bool { return a("string") == fixture2 }),
  154. ).Return(errors.New("fixture2"))
  155. assert.EqualError(t, mockedService.TheExampleMethodFunc(
  156. func(string) error { return fixture1 }), "fixture1")
  157. assert.EqualError(t, mockedService.TheExampleMethodFunc(
  158. func(string) error { return fixture2 }), "fixture2")
  159. }
  160. func Test_Mock_On_WithVariadicFunc(t *testing.T) {
  161. // make a test impl object
  162. var mockedService = new(TestExampleImplementation)
  163. c := mockedService.
  164. On("TheExampleMethodVariadic", []int{1, 2, 3}).
  165. Return(nil)
  166. assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  167. assert.Equal(t, 1, len(c.Arguments))
  168. assert.Equal(t, []int{1, 2, 3}, c.Arguments[0])
  169. assert.NotPanics(t, func() {
  170. mockedService.TheExampleMethodVariadic(1, 2, 3)
  171. })
  172. assert.Panics(t, func() {
  173. mockedService.TheExampleMethodVariadic(1, 2)
  174. })
  175. }
  176. func Test_Mock_On_WithVariadicFuncWithInterface(t *testing.T) {
  177. // make a test impl object
  178. var mockedService = new(TestExampleImplementation)
  179. c := mockedService.On("TheExampleMethodVariadicInterface", []interface{}{1, 2, 3}).
  180. Return(nil)
  181. assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  182. assert.Equal(t, 1, len(c.Arguments))
  183. assert.Equal(t, []interface{}{1, 2, 3}, c.Arguments[0])
  184. assert.NotPanics(t, func() {
  185. mockedService.TheExampleMethodVariadicInterface(1, 2, 3)
  186. })
  187. assert.Panics(t, func() {
  188. mockedService.TheExampleMethodVariadicInterface(1, 2)
  189. })
  190. }
  191. func Test_Mock_On_WithVariadicFuncWithEmptyInterfaceArray(t *testing.T) {
  192. // make a test impl object
  193. var mockedService = new(TestExampleImplementation)
  194. var expected []interface{}
  195. c := mockedService.
  196. On("TheExampleMethodVariadicInterface", expected).
  197. Return(nil)
  198. assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  199. assert.Equal(t, 1, len(c.Arguments))
  200. assert.Equal(t, expected, c.Arguments[0])
  201. assert.NotPanics(t, func() {
  202. mockedService.TheExampleMethodVariadicInterface()
  203. })
  204. assert.Panics(t, func() {
  205. mockedService.TheExampleMethodVariadicInterface(1, 2)
  206. })
  207. }
  208. func Test_Mock_On_WithFuncPanics(t *testing.T) {
  209. // make a test impl object
  210. var mockedService = new(TestExampleImplementation)
  211. assert.Panics(t, func() {
  212. mockedService.On("TheExampleMethodFunc", func(string) error { return nil })
  213. })
  214. }
  215. func Test_Mock_On_WithFuncTypeArg(t *testing.T) {
  216. // make a test impl object
  217. var mockedService = new(TestExampleImplementation)
  218. c := mockedService.
  219. On("TheExampleMethodFuncType", AnythingOfType("mock.ExampleFuncType")).
  220. Return(nil)
  221. assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  222. assert.Equal(t, 1, len(c.Arguments))
  223. assert.Equal(t, AnythingOfType("mock.ExampleFuncType"), c.Arguments[0])
  224. fn := func(string) error { return nil }
  225. assert.NotPanics(t, func() {
  226. mockedService.TheExampleMethodFuncType(fn)
  227. })
  228. }
  229. func Test_Mock_Return(t *testing.T) {
  230. // make a test impl object
  231. var mockedService = new(TestExampleImplementation)
  232. c := mockedService.
  233. On("TheExampleMethod", "A", "B", true).
  234. Return(1, "two", true)
  235. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  236. call := mockedService.ExpectedCalls[0]
  237. assert.Equal(t, "TheExampleMethod", call.Method)
  238. assert.Equal(t, "A", call.Arguments[0])
  239. assert.Equal(t, "B", call.Arguments[1])
  240. assert.Equal(t, true, call.Arguments[2])
  241. assert.Equal(t, 1, call.ReturnArguments[0])
  242. assert.Equal(t, "two", call.ReturnArguments[1])
  243. assert.Equal(t, true, call.ReturnArguments[2])
  244. assert.Equal(t, 0, call.Repeatability)
  245. assert.Nil(t, call.WaitFor)
  246. }
  247. func Test_Mock_Return_WaitUntil(t *testing.T) {
  248. // make a test impl object
  249. var mockedService = new(TestExampleImplementation)
  250. ch := time.After(time.Second)
  251. c := mockedService.Mock.
  252. On("TheExampleMethod", "A", "B", true).
  253. WaitUntil(ch).
  254. Return(1, "two", true)
  255. // assert that the call was created
  256. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  257. call := mockedService.ExpectedCalls[0]
  258. assert.Equal(t, "TheExampleMethod", call.Method)
  259. assert.Equal(t, "A", call.Arguments[0])
  260. assert.Equal(t, "B", call.Arguments[1])
  261. assert.Equal(t, true, call.Arguments[2])
  262. assert.Equal(t, 1, call.ReturnArguments[0])
  263. assert.Equal(t, "two", call.ReturnArguments[1])
  264. assert.Equal(t, true, call.ReturnArguments[2])
  265. assert.Equal(t, 0, call.Repeatability)
  266. assert.Equal(t, ch, call.WaitFor)
  267. }
  268. func Test_Mock_Return_After(t *testing.T) {
  269. // make a test impl object
  270. var mockedService = new(TestExampleImplementation)
  271. c := mockedService.Mock.
  272. On("TheExampleMethod", "A", "B", true).
  273. Return(1, "two", true).
  274. After(time.Second)
  275. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  276. call := mockedService.Mock.ExpectedCalls[0]
  277. assert.Equal(t, "TheExampleMethod", call.Method)
  278. assert.Equal(t, "A", call.Arguments[0])
  279. assert.Equal(t, "B", call.Arguments[1])
  280. assert.Equal(t, true, call.Arguments[2])
  281. assert.Equal(t, 1, call.ReturnArguments[0])
  282. assert.Equal(t, "two", call.ReturnArguments[1])
  283. assert.Equal(t, true, call.ReturnArguments[2])
  284. assert.Equal(t, 0, call.Repeatability)
  285. assert.NotEqual(t, nil, call.WaitFor)
  286. }
  287. func Test_Mock_Return_Run(t *testing.T) {
  288. // make a test impl object
  289. var mockedService = new(TestExampleImplementation)
  290. fn := func(args Arguments) {
  291. arg := args.Get(0).(*ExampleType)
  292. arg.ran = true
  293. }
  294. c := mockedService.Mock.
  295. On("TheExampleMethod3", AnythingOfType("*mock.ExampleType")).
  296. Return(nil).
  297. Run(fn)
  298. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  299. call := mockedService.Mock.ExpectedCalls[0]
  300. assert.Equal(t, "TheExampleMethod3", call.Method)
  301. assert.Equal(t, AnythingOfType("*mock.ExampleType"), call.Arguments[0])
  302. assert.Equal(t, nil, call.ReturnArguments[0])
  303. assert.Equal(t, 0, call.Repeatability)
  304. assert.NotEqual(t, nil, call.WaitFor)
  305. assert.NotNil(t, call.Run)
  306. et := ExampleType{}
  307. assert.Equal(t, false, et.ran)
  308. mockedService.TheExampleMethod3(&et)
  309. assert.Equal(t, true, et.ran)
  310. }
  311. func Test_Mock_Return_Run_Out_Of_Order(t *testing.T) {
  312. // make a test impl object
  313. var mockedService = new(TestExampleImplementation)
  314. f := func(args Arguments) {
  315. arg := args.Get(0).(*ExampleType)
  316. arg.ran = true
  317. }
  318. c := mockedService.Mock.
  319. On("TheExampleMethod3", AnythingOfType("*mock.ExampleType")).
  320. Run(f).
  321. Return(nil)
  322. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  323. call := mockedService.Mock.ExpectedCalls[0]
  324. assert.Equal(t, "TheExampleMethod3", call.Method)
  325. assert.Equal(t, AnythingOfType("*mock.ExampleType"), call.Arguments[0])
  326. assert.Equal(t, nil, call.ReturnArguments[0])
  327. assert.Equal(t, 0, call.Repeatability)
  328. assert.NotEqual(t, nil, call.WaitFor)
  329. assert.NotNil(t, call.Run)
  330. }
  331. func Test_Mock_Return_Once(t *testing.T) {
  332. // make a test impl object
  333. var mockedService = new(TestExampleImplementation)
  334. c := mockedService.On("TheExampleMethod", "A", "B", true).
  335. Return(1, "two", true).
  336. Once()
  337. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  338. call := mockedService.ExpectedCalls[0]
  339. assert.Equal(t, "TheExampleMethod", call.Method)
  340. assert.Equal(t, "A", call.Arguments[0])
  341. assert.Equal(t, "B", call.Arguments[1])
  342. assert.Equal(t, true, call.Arguments[2])
  343. assert.Equal(t, 1, call.ReturnArguments[0])
  344. assert.Equal(t, "two", call.ReturnArguments[1])
  345. assert.Equal(t, true, call.ReturnArguments[2])
  346. assert.Equal(t, 1, call.Repeatability)
  347. assert.Nil(t, call.WaitFor)
  348. }
  349. func Test_Mock_Return_Twice(t *testing.T) {
  350. // make a test impl object
  351. var mockedService = new(TestExampleImplementation)
  352. c := mockedService.
  353. On("TheExampleMethod", "A", "B", true).
  354. Return(1, "two", true).
  355. Twice()
  356. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  357. call := mockedService.ExpectedCalls[0]
  358. assert.Equal(t, "TheExampleMethod", call.Method)
  359. assert.Equal(t, "A", call.Arguments[0])
  360. assert.Equal(t, "B", call.Arguments[1])
  361. assert.Equal(t, true, call.Arguments[2])
  362. assert.Equal(t, 1, call.ReturnArguments[0])
  363. assert.Equal(t, "two", call.ReturnArguments[1])
  364. assert.Equal(t, true, call.ReturnArguments[2])
  365. assert.Equal(t, 2, call.Repeatability)
  366. assert.Nil(t, call.WaitFor)
  367. }
  368. func Test_Mock_Return_Times(t *testing.T) {
  369. // make a test impl object
  370. var mockedService = new(TestExampleImplementation)
  371. c := mockedService.
  372. On("TheExampleMethod", "A", "B", true).
  373. Return(1, "two", true).
  374. Times(5)
  375. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  376. call := mockedService.ExpectedCalls[0]
  377. assert.Equal(t, "TheExampleMethod", call.Method)
  378. assert.Equal(t, "A", call.Arguments[0])
  379. assert.Equal(t, "B", call.Arguments[1])
  380. assert.Equal(t, true, call.Arguments[2])
  381. assert.Equal(t, 1, call.ReturnArguments[0])
  382. assert.Equal(t, "two", call.ReturnArguments[1])
  383. assert.Equal(t, true, call.ReturnArguments[2])
  384. assert.Equal(t, 5, call.Repeatability)
  385. assert.Nil(t, call.WaitFor)
  386. }
  387. func Test_Mock_Return_Nothing(t *testing.T) {
  388. // make a test impl object
  389. var mockedService = new(TestExampleImplementation)
  390. c := mockedService.
  391. On("TheExampleMethod", "A", "B", true).
  392. Return()
  393. require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
  394. call := mockedService.ExpectedCalls[0]
  395. assert.Equal(t, "TheExampleMethod", call.Method)
  396. assert.Equal(t, "A", call.Arguments[0])
  397. assert.Equal(t, "B", call.Arguments[1])
  398. assert.Equal(t, true, call.Arguments[2])
  399. assert.Equal(t, 0, len(call.ReturnArguments))
  400. }
  401. func Test_Mock_findExpectedCall(t *testing.T) {
  402. m := new(Mock)
  403. m.On("One", 1).Return("one")
  404. m.On("Two", 2).Return("two")
  405. m.On("Two", 3).Return("three")
  406. f, c := m.findExpectedCall("Two", 3)
  407. if assert.Equal(t, 2, f) {
  408. if assert.NotNil(t, c) {
  409. assert.Equal(t, "Two", c.Method)
  410. assert.Equal(t, 3, c.Arguments[0])
  411. assert.Equal(t, "three", c.ReturnArguments[0])
  412. }
  413. }
  414. }
  415. func Test_Mock_findExpectedCall_For_Unknown_Method(t *testing.T) {
  416. m := new(Mock)
  417. m.On("One", 1).Return("one")
  418. m.On("Two", 2).Return("two")
  419. m.On("Two", 3).Return("three")
  420. f, _ := m.findExpectedCall("Two")
  421. assert.Equal(t, -1, f)
  422. }
  423. func Test_Mock_findExpectedCall_Respects_Repeatability(t *testing.T) {
  424. m := new(Mock)
  425. m.On("One", 1).Return("one")
  426. m.On("Two", 2).Return("two").Once()
  427. m.On("Two", 3).Return("three").Twice()
  428. m.On("Two", 3).Return("three").Times(8)
  429. f, c := m.findExpectedCall("Two", 3)
  430. if assert.Equal(t, 2, f) {
  431. if assert.NotNil(t, c) {
  432. assert.Equal(t, "Two", c.Method)
  433. assert.Equal(t, 3, c.Arguments[0])
  434. assert.Equal(t, "three", c.ReturnArguments[0])
  435. }
  436. }
  437. }
  438. func Test_callString(t *testing.T) {
  439. assert.Equal(t, `Method(int,bool,string)`, callString("Method", []interface{}{1, true, "something"}, false))
  440. }
  441. func Test_Mock_Called(t *testing.T) {
  442. var mockedService = new(TestExampleImplementation)
  443. mockedService.On("Test_Mock_Called", 1, 2, 3).Return(5, "6", true)
  444. returnArguments := mockedService.Called(1, 2, 3)
  445. if assert.Equal(t, 1, len(mockedService.Calls)) {
  446. assert.Equal(t, "Test_Mock_Called", mockedService.Calls[0].Method)
  447. assert.Equal(t, 1, mockedService.Calls[0].Arguments[0])
  448. assert.Equal(t, 2, mockedService.Calls[0].Arguments[1])
  449. assert.Equal(t, 3, mockedService.Calls[0].Arguments[2])
  450. }
  451. if assert.Equal(t, 3, len(returnArguments)) {
  452. assert.Equal(t, 5, returnArguments[0])
  453. assert.Equal(t, "6", returnArguments[1])
  454. assert.Equal(t, true, returnArguments[2])
  455. }
  456. }
  457. func asyncCall(m *Mock, ch chan Arguments) {
  458. ch <- m.Called(1, 2, 3)
  459. }
  460. func Test_Mock_Called_blocks(t *testing.T) {
  461. var mockedService = new(TestExampleImplementation)
  462. mockedService.Mock.On("asyncCall", 1, 2, 3).Return(5, "6", true).After(2 * time.Millisecond)
  463. ch := make(chan Arguments)
  464. go asyncCall(&mockedService.Mock, ch)
  465. select {
  466. case <-ch:
  467. t.Fatal("should have waited")
  468. case <-time.After(1 * time.Millisecond):
  469. }
  470. returnArguments := <-ch
  471. if assert.Equal(t, 1, len(mockedService.Mock.Calls)) {
  472. assert.Equal(t, "asyncCall", mockedService.Mock.Calls[0].Method)
  473. assert.Equal(t, 1, mockedService.Mock.Calls[0].Arguments[0])
  474. assert.Equal(t, 2, mockedService.Mock.Calls[0].Arguments[1])
  475. assert.Equal(t, 3, mockedService.Mock.Calls[0].Arguments[2])
  476. }
  477. if assert.Equal(t, 3, len(returnArguments)) {
  478. assert.Equal(t, 5, returnArguments[0])
  479. assert.Equal(t, "6", returnArguments[1])
  480. assert.Equal(t, true, returnArguments[2])
  481. }
  482. }
  483. func Test_Mock_Called_For_Bounded_Repeatability(t *testing.T) {
  484. var mockedService = new(TestExampleImplementation)
  485. mockedService.
  486. On("Test_Mock_Called_For_Bounded_Repeatability", 1, 2, 3).
  487. Return(5, "6", true).
  488. Once()
  489. mockedService.
  490. On("Test_Mock_Called_For_Bounded_Repeatability", 1, 2, 3).
  491. Return(-1, "hi", false)
  492. returnArguments1 := mockedService.Called(1, 2, 3)
  493. returnArguments2 := mockedService.Called(1, 2, 3)
  494. if assert.Equal(t, 2, len(mockedService.Calls)) {
  495. assert.Equal(t, "Test_Mock_Called_For_Bounded_Repeatability", mockedService.Calls[0].Method)
  496. assert.Equal(t, 1, mockedService.Calls[0].Arguments[0])
  497. assert.Equal(t, 2, mockedService.Calls[0].Arguments[1])
  498. assert.Equal(t, 3, mockedService.Calls[0].Arguments[2])
  499. assert.Equal(t, "Test_Mock_Called_For_Bounded_Repeatability", mockedService.Calls[1].Method)
  500. assert.Equal(t, 1, mockedService.Calls[1].Arguments[0])
  501. assert.Equal(t, 2, mockedService.Calls[1].Arguments[1])
  502. assert.Equal(t, 3, mockedService.Calls[1].Arguments[2])
  503. }
  504. if assert.Equal(t, 3, len(returnArguments1)) {
  505. assert.Equal(t, 5, returnArguments1[0])
  506. assert.Equal(t, "6", returnArguments1[1])
  507. assert.Equal(t, true, returnArguments1[2])
  508. }
  509. if assert.Equal(t, 3, len(returnArguments2)) {
  510. assert.Equal(t, -1, returnArguments2[0])
  511. assert.Equal(t, "hi", returnArguments2[1])
  512. assert.Equal(t, false, returnArguments2[2])
  513. }
  514. }
  515. func Test_Mock_Called_For_SetTime_Expectation(t *testing.T) {
  516. var mockedService = new(TestExampleImplementation)
  517. mockedService.On("TheExampleMethod", 1, 2, 3).Return(5, "6", true).Times(4)
  518. mockedService.TheExampleMethod(1, 2, 3)
  519. mockedService.TheExampleMethod(1, 2, 3)
  520. mockedService.TheExampleMethod(1, 2, 3)
  521. mockedService.TheExampleMethod(1, 2, 3)
  522. assert.Panics(t, func() {
  523. mockedService.TheExampleMethod(1, 2, 3)
  524. })
  525. }
  526. func Test_Mock_Called_Unexpected(t *testing.T) {
  527. var mockedService = new(TestExampleImplementation)
  528. // make sure it panics if no expectation was made
  529. assert.Panics(t, func() {
  530. mockedService.Called(1, 2, 3)
  531. }, "Calling unexpected method should panic")
  532. }
  533. func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
  534. var mockedService1 = new(TestExampleImplementation)
  535. var mockedService2 = new(TestExampleImplementation)
  536. var mockedService3 = new(TestExampleImplementation)
  537. mockedService1.On("Test_AssertExpectationsForObjects_Helper", 1).Return()
  538. mockedService2.On("Test_AssertExpectationsForObjects_Helper", 2).Return()
  539. mockedService3.On("Test_AssertExpectationsForObjects_Helper", 3).Return()
  540. mockedService1.Called(1)
  541. mockedService2.Called(2)
  542. mockedService3.Called(3)
  543. assert.True(t, AssertExpectationsForObjects(t, mockedService1.Mock, mockedService2.Mock, mockedService3.Mock))
  544. assert.True(t, AssertExpectationsForObjects(t, mockedService1, mockedService2, mockedService3))
  545. }
  546. func Test_AssertExpectationsForObjects_Helper_Failed(t *testing.T) {
  547. var mockedService1 = new(TestExampleImplementation)
  548. var mockedService2 = new(TestExampleImplementation)
  549. var mockedService3 = new(TestExampleImplementation)
  550. mockedService1.On("Test_AssertExpectationsForObjects_Helper_Failed", 1).Return()
  551. mockedService2.On("Test_AssertExpectationsForObjects_Helper_Failed", 2).Return()
  552. mockedService3.On("Test_AssertExpectationsForObjects_Helper_Failed", 3).Return()
  553. mockedService1.Called(1)
  554. mockedService3.Called(3)
  555. tt := new(testing.T)
  556. assert.False(t, AssertExpectationsForObjects(tt, mockedService1.Mock, mockedService2.Mock, mockedService3.Mock))
  557. assert.False(t, AssertExpectationsForObjects(tt, mockedService1, mockedService2, mockedService3))
  558. }
  559. func Test_Mock_AssertExpectations(t *testing.T) {
  560. var mockedService = new(TestExampleImplementation)
  561. mockedService.On("Test_Mock_AssertExpectations", 1, 2, 3).Return(5, 6, 7)
  562. tt := new(testing.T)
  563. assert.False(t, mockedService.AssertExpectations(tt))
  564. // make the call now
  565. mockedService.Called(1, 2, 3)
  566. // now assert expectations
  567. assert.True(t, mockedService.AssertExpectations(tt))
  568. }
  569. func Test_Mock_AssertExpectations_Placeholder_NoArgs(t *testing.T) {
  570. var mockedService = new(TestExampleImplementation)
  571. mockedService.On("Test_Mock_AssertExpectations_Placeholder_NoArgs").Return(5, 6, 7).Once()
  572. mockedService.On("Test_Mock_AssertExpectations_Placeholder_NoArgs").Return(7, 6, 5)
  573. tt := new(testing.T)
  574. assert.False(t, mockedService.AssertExpectations(tt))
  575. // make the call now
  576. mockedService.Called()
  577. // now assert expectations
  578. assert.True(t, mockedService.AssertExpectations(tt))
  579. }
  580. func Test_Mock_AssertExpectations_Placeholder(t *testing.T) {
  581. var mockedService = new(TestExampleImplementation)
  582. mockedService.On("Test_Mock_AssertExpectations_Placeholder", 1, 2, 3).Return(5, 6, 7).Once()
  583. mockedService.On("Test_Mock_AssertExpectations_Placeholder", 3, 2, 1).Return(7, 6, 5)
  584. tt := new(testing.T)
  585. assert.False(t, mockedService.AssertExpectations(tt))
  586. // make the call now
  587. mockedService.Called(1, 2, 3)
  588. // now assert expectations
  589. assert.False(t, mockedService.AssertExpectations(tt))
  590. // make call to the second expectation
  591. mockedService.Called(3, 2, 1)
  592. // now assert expectations again
  593. assert.True(t, mockedService.AssertExpectations(tt))
  594. }
  595. func Test_Mock_AssertExpectations_With_Pointers(t *testing.T) {
  596. var mockedService = new(TestExampleImplementation)
  597. mockedService.On("Test_Mock_AssertExpectations_With_Pointers", &struct{ Foo int }{1}).Return(1)
  598. mockedService.On("Test_Mock_AssertExpectations_With_Pointers", &struct{ Foo int }{2}).Return(2)
  599. tt := new(testing.T)
  600. assert.False(t, mockedService.AssertExpectations(tt))
  601. s := struct{ Foo int }{1}
  602. // make the calls now
  603. mockedService.Called(&s)
  604. s.Foo = 2
  605. mockedService.Called(&s)
  606. // now assert expectations
  607. assert.True(t, mockedService.AssertExpectations(tt))
  608. }
  609. func Test_Mock_AssertExpectationsCustomType(t *testing.T) {
  610. var mockedService = new(TestExampleImplementation)
  611. mockedService.On("TheExampleMethod3", AnythingOfType("*mock.ExampleType")).Return(nil).Once()
  612. tt := new(testing.T)
  613. assert.False(t, mockedService.AssertExpectations(tt))
  614. // make the call now
  615. mockedService.TheExampleMethod3(&ExampleType{})
  616. // now assert expectations
  617. assert.True(t, mockedService.AssertExpectations(tt))
  618. }
  619. func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) {
  620. var mockedService = new(TestExampleImplementation)
  621. mockedService.On("Test_Mock_AssertExpectations_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Twice()
  622. tt := new(testing.T)
  623. assert.False(t, mockedService.AssertExpectations(tt))
  624. // make the call now
  625. mockedService.Called(1, 2, 3)
  626. assert.False(t, mockedService.AssertExpectations(tt))
  627. mockedService.Called(1, 2, 3)
  628. // now assert expectations
  629. assert.True(t, mockedService.AssertExpectations(tt))
  630. }
  631. func Test_Mock_TwoCallsWithDifferentArguments(t *testing.T) {
  632. var mockedService = new(TestExampleImplementation)
  633. mockedService.On("Test_Mock_TwoCallsWithDifferentArguments", 1, 2, 3).Return(5, 6, 7)
  634. mockedService.On("Test_Mock_TwoCallsWithDifferentArguments", 4, 5, 6).Return(5, 6, 7)
  635. args1 := mockedService.Called(1, 2, 3)
  636. assert.Equal(t, 5, args1.Int(0))
  637. assert.Equal(t, 6, args1.Int(1))
  638. assert.Equal(t, 7, args1.Int(2))
  639. args2 := mockedService.Called(4, 5, 6)
  640. assert.Equal(t, 5, args2.Int(0))
  641. assert.Equal(t, 6, args2.Int(1))
  642. assert.Equal(t, 7, args2.Int(2))
  643. }
  644. func Test_Mock_AssertNumberOfCalls(t *testing.T) {
  645. var mockedService = new(TestExampleImplementation)
  646. mockedService.On("Test_Mock_AssertNumberOfCalls", 1, 2, 3).Return(5, 6, 7)
  647. mockedService.Called(1, 2, 3)
  648. assert.True(t, mockedService.AssertNumberOfCalls(t, "Test_Mock_AssertNumberOfCalls", 1))
  649. mockedService.Called(1, 2, 3)
  650. assert.True(t, mockedService.AssertNumberOfCalls(t, "Test_Mock_AssertNumberOfCalls", 2))
  651. }
  652. func Test_Mock_AssertCalled(t *testing.T) {
  653. var mockedService = new(TestExampleImplementation)
  654. mockedService.On("Test_Mock_AssertCalled", 1, 2, 3).Return(5, 6, 7)
  655. mockedService.Called(1, 2, 3)
  656. assert.True(t, mockedService.AssertCalled(t, "Test_Mock_AssertCalled", 1, 2, 3))
  657. }
  658. func Test_Mock_AssertCalled_WithAnythingOfTypeArgument(t *testing.T) {
  659. var mockedService = new(TestExampleImplementation)
  660. mockedService.
  661. On("Test_Mock_AssertCalled_WithAnythingOfTypeArgument", Anything, Anything, Anything).
  662. Return()
  663. mockedService.Called(1, "two", []uint8("three"))
  664. assert.True(t, mockedService.AssertCalled(t, "Test_Mock_AssertCalled_WithAnythingOfTypeArgument", AnythingOfType("int"), AnythingOfType("string"), AnythingOfType("[]uint8")))
  665. }
  666. func Test_Mock_AssertCalled_WithArguments(t *testing.T) {
  667. var mockedService = new(TestExampleImplementation)
  668. mockedService.On("Test_Mock_AssertCalled_WithArguments", 1, 2, 3).Return(5, 6, 7)
  669. mockedService.Called(1, 2, 3)
  670. tt := new(testing.T)
  671. assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments", 1, 2, 3))
  672. assert.False(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments", 2, 3, 4))
  673. }
  674. func Test_Mock_AssertCalled_WithArguments_With_Repeatability(t *testing.T) {
  675. var mockedService = new(TestExampleImplementation)
  676. mockedService.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Once()
  677. mockedService.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 2, 3, 4).Return(5, 6, 7).Once()
  678. mockedService.Called(1, 2, 3)
  679. mockedService.Called(2, 3, 4)
  680. tt := new(testing.T)
  681. assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 1, 2, 3))
  682. assert.True(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 2, 3, 4))
  683. assert.False(t, mockedService.AssertCalled(tt, "Test_Mock_AssertCalled_WithArguments_With_Repeatability", 3, 4, 5))
  684. }
  685. func Test_Mock_AssertNotCalled(t *testing.T) {
  686. var mockedService = new(TestExampleImplementation)
  687. mockedService.On("Test_Mock_AssertNotCalled", 1, 2, 3).Return(5, 6, 7)
  688. mockedService.Called(1, 2, 3)
  689. assert.True(t, mockedService.AssertNotCalled(t, "Test_Mock_NotCalled"))
  690. }
  691. /*
  692. Arguments helper methods
  693. */
  694. func Test_Arguments_Get(t *testing.T) {
  695. var args = Arguments([]interface{}{"string", 123, true})
  696. assert.Equal(t, "string", args.Get(0).(string))
  697. assert.Equal(t, 123, args.Get(1).(int))
  698. assert.Equal(t, true, args.Get(2).(bool))
  699. }
  700. func Test_Arguments_Is(t *testing.T) {
  701. var args = Arguments([]interface{}{"string", 123, true})
  702. assert.True(t, args.Is("string", 123, true))
  703. assert.False(t, args.Is("wrong", 456, false))
  704. }
  705. func Test_Arguments_Diff(t *testing.T) {
  706. var args = Arguments([]interface{}{"Hello World", 123, true})
  707. var diff string
  708. var count int
  709. diff, count = args.Diff([]interface{}{"Hello World", 456, "false"})
  710. assert.Equal(t, 2, count)
  711. assert.Contains(t, diff, `%!s(int=456) != %!s(int=123)`)
  712. assert.Contains(t, diff, `false != %!s(bool=true)`)
  713. }
  714. func Test_Arguments_Diff_DifferentNumberOfArgs(t *testing.T) {
  715. var args = Arguments([]interface{}{"string", 123, true})
  716. var diff string
  717. var count int
  718. diff, count = args.Diff([]interface{}{"string", 456, "false", "extra"})
  719. assert.Equal(t, 3, count)
  720. assert.Contains(t, diff, `extra != (Missing)`)
  721. }
  722. func Test_Arguments_Diff_WithAnythingArgument(t *testing.T) {
  723. var args = Arguments([]interface{}{"string", 123, true})
  724. var count int
  725. _, count = args.Diff([]interface{}{"string", Anything, true})
  726. assert.Equal(t, 0, count)
  727. }
  728. func Test_Arguments_Diff_WithAnythingArgument_InActualToo(t *testing.T) {
  729. var args = Arguments([]interface{}{"string", Anything, true})
  730. var count int
  731. _, count = args.Diff([]interface{}{"string", 123, true})
  732. assert.Equal(t, 0, count)
  733. }
  734. func Test_Arguments_Diff_WithAnythingOfTypeArgument(t *testing.T) {
  735. var args = Arguments([]interface{}{"string", AnythingOfType("int"), true})
  736. var count int
  737. _, count = args.Diff([]interface{}{"string", 123, true})
  738. assert.Equal(t, 0, count)
  739. }
  740. func Test_Arguments_Diff_WithAnythingOfTypeArgument_Failing(t *testing.T) {
  741. var args = Arguments([]interface{}{"string", AnythingOfType("string"), true})
  742. var count int
  743. var diff string
  744. diff, count = args.Diff([]interface{}{"string", 123, true})
  745. assert.Equal(t, 1, count)
  746. assert.Contains(t, diff, `string != type int - %!s(int=123)`)
  747. }
  748. func Test_Arguments_Diff_WithArgMatcher(t *testing.T) {
  749. matchFn := func(a int) bool {
  750. return a == 123
  751. }
  752. var args = Arguments([]interface{}{"string", MatchedBy(matchFn), true})
  753. diff, count := args.Diff([]interface{}{"string", 124, true})
  754. assert.Equal(t, 1, count)
  755. assert.Contains(t, diff, `%!s(int=124) not matched by func(int) bool`)
  756. diff, count = args.Diff([]interface{}{"string", false, true})
  757. assert.Equal(t, 1, count)
  758. assert.Contains(t, diff, `%!s(bool=false) not matched by func(int) bool`)
  759. diff, count = args.Diff([]interface{}{"string", 123, false})
  760. assert.Contains(t, diff, `%!s(int=123) matched by func(int) bool`)
  761. diff, count = args.Diff([]interface{}{"string", 123, true})
  762. assert.Equal(t, 0, count)
  763. assert.Contains(t, diff, `No differences.`)
  764. }
  765. func Test_Arguments_Assert(t *testing.T) {
  766. var args = Arguments([]interface{}{"string", 123, true})
  767. assert.True(t, args.Assert(t, "string", 123, true))
  768. }
  769. func Test_Arguments_String_Representation(t *testing.T) {
  770. var args = Arguments([]interface{}{"string", 123, true})
  771. assert.Equal(t, `string,int,bool`, args.String())
  772. }
  773. func Test_Arguments_String(t *testing.T) {
  774. var args = Arguments([]interface{}{"string", 123, true})
  775. assert.Equal(t, "string", args.String(0))
  776. }
  777. func Test_Arguments_Error(t *testing.T) {
  778. var err = errors.New("An Error")
  779. var args = Arguments([]interface{}{"string", 123, true, err})
  780. assert.Equal(t, err, args.Error(3))
  781. }
  782. func Test_Arguments_Error_Nil(t *testing.T) {
  783. var args = Arguments([]interface{}{"string", 123, true, nil})
  784. assert.Equal(t, nil, args.Error(3))
  785. }
  786. func Test_Arguments_Int(t *testing.T) {
  787. var args = Arguments([]interface{}{"string", 123, true})
  788. assert.Equal(t, 123, args.Int(1))
  789. }
  790. func Test_Arguments_Bool(t *testing.T) {
  791. var args = Arguments([]interface{}{"string", 123, true})
  792. assert.Equal(t, true, args.Bool(2))
  793. }
  794. func Test_WaitUntil_Parallel(t *testing.T) {
  795. // make a test impl object
  796. var mockedService *TestExampleImplementation = new(TestExampleImplementation)
  797. ch1 := make(chan time.Time)
  798. ch2 := make(chan time.Time)
  799. mockedService.Mock.On("TheExampleMethod2", true).Return().WaitUntil(ch2).Run(func(args Arguments) {
  800. ch1 <- time.Now()
  801. })
  802. mockedService.Mock.On("TheExampleMethod2", false).Return().WaitUntil(ch1)
  803. // Lock both goroutines on the .WaitUntil method
  804. go func() {
  805. mockedService.TheExampleMethod2(false)
  806. }()
  807. go func() {
  808. mockedService.TheExampleMethod2(true)
  809. }()
  810. // Allow the first call to execute, so the second one executes afterwards
  811. ch2 <- time.Now()
  812. }