API de comptabilité horaire.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

paginated_test.rb 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. require "test_helper"
  2. require_relative "../../app/models/paginated"
  3. # fake model, fake behavior, etc.
  4. class FakePaginated
  5. DATA = ("a".."z").to_a
  6. # fake inspection
  7. def self.data
  8. @@data
  9. end
  10. # fake a model
  11. def self.scope(scope_name, callback)
  12. define_singleton_method scope_name, callback
  13. end
  14. # fake a model
  15. def self.reset!
  16. @@data = DATA
  17. end
  18. # fake a model
  19. def self.offset(start)
  20. @@data = @@data[start..]
  21. self
  22. end
  23. # fake a model
  24. def self.limit(count)
  25. @@data = @@data[...count]
  26. self
  27. end
  28. def self.count_by_page
  29. 3
  30. end
  31. include Paginated
  32. end
  33. class PaginatedTest < ActiveSupport::TestCase
  34. def assert_valid_pagination(paginated, count:)
  35. assert_equal count, paginated.data.size
  36. end
  37. test "paginated with placeholder" do
  38. FakePaginated.reset!
  39. test_empty_params = FakePaginated.paginate({})
  40. assert_valid_pagination test_empty_params, count: FakePaginated.count_by_page
  41. assert_equal %w[a b c], test_empty_params.data
  42. FakePaginated.reset!
  43. test_valid_params = FakePaginated.paginate({ page: 1 })
  44. assert_valid_pagination test_valid_params, count: FakePaginated.count_by_page
  45. assert_equal %w[d e f], test_valid_params.data
  46. FakePaginated.reset!
  47. test_valid_params = FakePaginated.paginate({ page: 4 })
  48. assert_valid_pagination test_valid_params, count: FakePaginated.count_by_page
  49. assert_equal %w[m n o], test_valid_params.data
  50. FakePaginated.reset!
  51. test_invalid_params = FakePaginated.paginate({ page: "what" })
  52. assert_valid_pagination test_invalid_params, count: FakePaginated.count_by_page
  53. assert_equal %w[a b c], test_invalid_params.data
  54. end
  55. end