test_helper.rb 3.0 KB

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