test_helper.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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("https://this-is-my-test-shop.myshopify.com", "token_test_helper", :no_version)
  38. ShopifyAPI::Base.activate_session(session)
  39. end
  40. def teardown
  41. FakeWeb.clean_registry
  42. ShopifyAPI::Base.clear_session
  43. ShopifyAPI::Base.site = nil
  44. ShopifyAPI::Base.password = nil
  45. ShopifyAPI::Base.user = nil
  46. end
  47. # Custom Assertions
  48. def assert_not(expression)
  49. refute expression, "Expected <#{expression}> to be false!"
  50. end
  51. def assert_nothing_raised
  52. yield
  53. end
  54. def assert_not_includes(array, value)
  55. refute array.include?(value)
  56. end
  57. def assert_includes(array, value)
  58. assert array.include?(value)
  59. end
  60. def load_fixture(name, format=:json)
  61. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  62. end
  63. def assert_request_body(expected)
  64. assert_equal expected, FakeWeb.last_request.body
  65. end
  66. def fake(endpoint, options={})
  67. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  68. format = options.delete(:format) || :json
  69. method = options.delete(:method) || :get
  70. api_version = options.delete(:api_version) || ShopifyAPI::ApiVersion.coerce_to_version(:no_version)
  71. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  72. url = if options.has_key?(:url)
  73. options[:url]
  74. else
  75. "https://this-is-my-test-shop.myshopify.com#{api_version.construct_api_path("#{endpoint}#{extension}")}"
  76. end
  77. FakeWeb.register_uri(method, url, {:body => body, :status => 200, :content_type => "text/#{format}", :content_length => 1}.merge(options))
  78. end
  79. def ar_version_before?(version_string)
  80. Gem::Version.new(ActiveResource::VERSION::STRING) < Gem::Version.new(version_string)
  81. end
  82. def ar_version_after?(version_string)
  83. Gem::Version.new(version_string) < Gem::Version.new(ActiveResource::VERSION::STRING)
  84. end
  85. end