test_helper.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. require 'rubygems'
  2. require 'minitest/autorun'
  3. require 'webmock/minitest'
  4. require_relative 'lib/webmock_extensions/last_request'
  5. require 'mocha/setup'
  6. require 'pry'
  7. $LOAD_PATH.unshift(File.dirname(__FILE__))
  8. $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  9. require 'shopify_api'
  10. WebMock.disable_net_connect!
  11. # setup ShopifyAPI with fake api_key and secret
  12. module Test
  13. module Unit
  14. class TestCase < Minitest::Test
  15. def self.test(string, &block)
  16. define_method("test_#{string}", &block)
  17. end
  18. def self.should(string, &block)
  19. self.test("should_#{string}", &block)
  20. end
  21. def self.context(string)
  22. yield
  23. end
  24. def setup
  25. ActiveResource::Base.format = :json
  26. ShopifyAPI.constants.each do |const|
  27. begin
  28. const = mod.const_get(const)
  29. const.format = :json if const.respond_to?(:format=)
  30. rescue NameError
  31. end
  32. end
  33. ShopifyAPI::ApiVersion.define_version(ShopifyAPI::ApiVersion::Release.new('2019-01'))
  34. ShopifyAPI::Base.clear_session
  35. session = ShopifyAPI::Session.new(
  36. domain: "https://this-is-my-test-shop.myshopify.com",
  37. token: "token_test_helper",
  38. api_version: '2019-01',
  39. )
  40. ShopifyAPI::Base.activate_session(session)
  41. end
  42. def teardown
  43. ShopifyAPI::Base.clear_session
  44. ShopifyAPI::Base.site = nil
  45. ShopifyAPI::Base.password = nil
  46. ShopifyAPI::Base.user = nil
  47. ShopifyAPI::ApiVersion.clear_defined_versions
  48. ShopifyAPI::ApiVersion.define_known_versions
  49. end
  50. # Custom Assertions
  51. def assert_not(expression)
  52. refute expression, "Expected <#{expression}> to be false!"
  53. end
  54. def assert_nothing_raised
  55. yield
  56. end
  57. def assert_not_includes(array, value)
  58. refute array.include?(value)
  59. end
  60. def assert_includes(array, value)
  61. assert array.include?(value)
  62. end
  63. def load_fixture(name, format=:json)
  64. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  65. end
  66. def assert_request_body(expected)
  67. assert_equal expected, WebMock.last_request.body
  68. end
  69. def fake(endpoint, options={})
  70. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  71. format = options.delete(:format) || :json
  72. method = options.delete(:method) || :get
  73. api_version = options.delete(:api_version) || ShopifyAPI::ApiVersion.coerce_to_version('2019-01')
  74. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  75. status = options.delete(:status) || 200
  76. url = if options.has_key?(:url)
  77. options[:url]
  78. else
  79. "https://this-is-my-test-shop.myshopify.com#{api_version.construct_api_path("#{endpoint}#{extension}")}"
  80. end
  81. WebMock.stub_request(method, url).to_return(
  82. body: body, status: status, headers: { content_type: "text/#{format}", content_length: 1 }.merge(options)
  83. )
  84. end
  85. def ar_version_before?(version_string)
  86. Gem::Version.new(ActiveResource::VERSION::STRING) < Gem::Version.new(version_string)
  87. end
  88. def ar_version_after?(version_string)
  89. Gem::Version.new(version_string) < Gem::Version.new(ActiveResource::VERSION::STRING)
  90. end
  91. end
  92. end
  93. end