test_helper.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require 'rubygems'
  2. require 'minitest/autorun'
  3. require 'fakeweb'
  4. require 'mocha/setup'
  5. require 'pry'
  6. $LOAD_PATH.unshift(File.dirname(__FILE__))
  7. $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  8. require 'shopify_api'
  9. FakeWeb.allow_net_connect = false
  10. # setup ShopifyAPI with fake api_key and secret
  11. module Test
  12. module Unit
  13. end
  14. end
  15. class Test::Unit::TestCase < Minitest::Unit::TestCase
  16. def self.test(string, &block)
  17. define_method("test_#{string}", &block)
  18. end
  19. def self.should(string, &block)
  20. self.test("should_#{string}", &block)
  21. end
  22. def self.context(string)
  23. yield
  24. end
  25. def setup
  26. ActiveResource::Base.format = :json
  27. [ShopifyAPI, ShopifyAPI::Ping].each do |mod|
  28. mod.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. end
  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: :no_version
  41. )
  42. ShopifyAPI::Base.activate_session(session)
  43. end
  44. def teardown
  45. FakeWeb.clean_registry
  46. ShopifyAPI::Base.clear_session
  47. ShopifyAPI::Base.site = nil
  48. ShopifyAPI::Base.password = nil
  49. ShopifyAPI::Base.user = nil
  50. end
  51. # Custom Assertions
  52. def assert_not(expression)
  53. refute expression, "Expected <#{expression}> to be false!"
  54. end
  55. def assert_nothing_raised
  56. yield
  57. end
  58. def assert_not_includes(array, value)
  59. refute array.include?(value)
  60. end
  61. def assert_includes(array, value)
  62. assert array.include?(value)
  63. end
  64. def load_fixture(name, format=:json)
  65. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  66. end
  67. def assert_request_body(expected)
  68. assert_equal expected, FakeWeb.last_request.body
  69. end
  70. def fake(endpoint, options={})
  71. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  72. format = options.delete(:format) || :json
  73. method = options.delete(:method) || :get
  74. api_version = options.delete(:api_version) || ShopifyAPI::ApiVersion.coerce_to_version(:no_version)
  75. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  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. FakeWeb.register_uri(method, url, {:body => body, :status => 200, :content_type => "text/#{format}", :content_length => 1}.merge(options))
  82. end
  83. def ar_version_before?(version_string)
  84. Gem::Version.new(ActiveResource::VERSION::STRING) < Gem::Version.new(version_string)
  85. end
  86. def ar_version_after?(version_string)
  87. Gem::Version.new(version_string) < Gem::Version.new(ActiveResource::VERSION::STRING)
  88. end
  89. end