test_helper.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.constants.each do |const|
  27. begin
  28. const = "ShopifyAPI::#{const}".constantize
  29. const.format = :json if const.respond_to?(:format=)
  30. rescue NameError
  31. end
  32. end
  33. ShopifyAPI::Base.clear_session
  34. ShopifyAPI::Base.site = "https://this-is-my-test-shop.myshopify.com/admin"
  35. ShopifyAPI::Base.password = nil
  36. ShopifyAPI::Base.user = nil
  37. end
  38. def teardown
  39. FakeWeb.clean_registry
  40. ShopifyAPI::Base.clear_session
  41. ShopifyAPI::Base.site = nil
  42. ShopifyAPI::Base.password = nil
  43. ShopifyAPI::Base.user = nil
  44. end
  45. # Custom Assertions
  46. def assert_not(expression)
  47. refute expression, "Expected <#{expression}> to be false!"
  48. end
  49. def assert_nothing_raised
  50. yield
  51. end
  52. def assert_not_includes(array, value)
  53. refute array.include?(value)
  54. end
  55. def assert_includes(array, value)
  56. assert array.include?(value)
  57. end
  58. def load_fixture(name, format=:json)
  59. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  60. end
  61. def assert_request_body(expected)
  62. assert_equal expected, FakeWeb.last_request.body
  63. end
  64. def fake(endpoint, options={})
  65. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  66. format = options.delete(:format) || :json
  67. method = options.delete(:method) || :get
  68. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  69. url = if options.has_key?(:url)
  70. options[:url]
  71. else
  72. "https://this-is-my-test-shop.myshopify.com/admin/#{endpoint}#{extension}"
  73. end
  74. FakeWeb.register_uri(method, url, {:body => body, :status => 200, :content_type => "text/#{format}", :content_length => 1}.merge(options))
  75. end
  76. end