test_helper.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. require 'rubygems'
  2. require 'minitest/autorun'
  3. require 'fakeweb'
  4. require 'mocha/setup'
  5. $LOAD_PATH.unshift(File.dirname(__FILE__))
  6. $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  7. require 'shopify_api'
  8. FakeWeb.allow_net_connect = false
  9. # setup ShopifyAPI with fake api_key and secret
  10. module Test
  11. module Unit
  12. end
  13. end
  14. class Test::Unit::TestCase < Minitest::Unit::TestCase
  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, ShopifyAPI::Ping].each do |mod|
  27. mod.constants.each do |const|
  28. begin
  29. const = mod.const_get(const)
  30. const.format = :json if const.respond_to?(:format=)
  31. rescue NameError
  32. end
  33. end
  34. end
  35. ShopifyAPI::Base.clear_session
  36. ShopifyAPI::Base.site = "https://this-is-my-test-shop.myshopify.com/admin"
  37. ShopifyAPI::Base.password = nil
  38. ShopifyAPI::Base.user = nil
  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. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  71. url = if options.has_key?(:url)
  72. options[:url]
  73. else
  74. "https://this-is-my-test-shop.myshopify.com/admin/#{endpoint}#{extension}"
  75. end
  76. FakeWeb.register_uri(method, url, {:body => body, :status => 200, :content_type => "text/#{format}", :content_length => 1}.merge(options))
  77. end
  78. end