test_helper.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. ShopifyAPI::Base.site = "https://this-is-my-test-shop.myshopify.com/admin"
  38. ShopifyAPI::Base.password = nil
  39. ShopifyAPI::Base.user = nil
  40. end
  41. def teardown
  42. FakeWeb.clean_registry
  43. ShopifyAPI::Base.clear_session
  44. ShopifyAPI::Base.site = nil
  45. ShopifyAPI::Base.password = nil
  46. ShopifyAPI::Base.user = nil
  47. end
  48. # Custom Assertions
  49. def assert_not(expression)
  50. refute expression, "Expected <#{expression}> to be false!"
  51. end
  52. def assert_nothing_raised
  53. yield
  54. end
  55. def assert_not_includes(array, value)
  56. refute array.include?(value)
  57. end
  58. def assert_includes(array, value)
  59. assert array.include?(value)
  60. end
  61. def load_fixture(name, format=:json)
  62. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  63. end
  64. def assert_request_body(expected)
  65. assert_equal expected, FakeWeb.last_request.body
  66. end
  67. def fake(endpoint, options={})
  68. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  69. format = options.delete(:format) || :json
  70. method = options.delete(:method) || :get
  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/admin/#{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. end