test_helper.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. end
  41. # Custom Assertions
  42. def assert_not(expression)
  43. refute expression, "Expected <#{expression}> to be false!"
  44. end
  45. def assert_nothing_raised
  46. yield
  47. end
  48. def assert_not_includes(array, value)
  49. refute array.include?(value)
  50. end
  51. def assert_includes(array, value)
  52. assert array.include?(value)
  53. end
  54. def load_fixture(name, format=:json)
  55. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  56. end
  57. def assert_request_body(expected)
  58. assert_equal expected, FakeWeb.last_request.body
  59. end
  60. def fake(endpoint, options={})
  61. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  62. format = options.delete(:format) || :json
  63. method = options.delete(:method) || :get
  64. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  65. url = if options.has_key?(:url)
  66. options[:url]
  67. else
  68. "https://this-is-my-test-shop.myshopify.com/admin/#{endpoint}#{extension}"
  69. end
  70. FakeWeb.register_uri(method, url, {:body => body, :status => 200, :content_type => "text/#{format}", :content_length => 1}.merge(options))
  71. end
  72. end