test_helper.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. require 'rubygems'
  2. require 'test/unit'
  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. class Test::Unit::TestCase
  11. def self.test(string, &block)
  12. define_method("test:#{string}", &block)
  13. end
  14. def self.should(string, &block)
  15. self.test("should_#{string}", &block)
  16. end
  17. def self.context(string)
  18. yield
  19. end
  20. def setup
  21. ActiveResource::Base.format = :json
  22. ShopifyAPI.constants.each do |const|
  23. begin
  24. const = "ShopifyAPI::#{const}".constantize
  25. const.format = :json if const.respond_to?(:format=)
  26. rescue NameError
  27. end
  28. end
  29. ShopifyAPI::Base.clear_session
  30. ShopifyAPI::Base.site = "http://localhost/admin"
  31. ShopifyAPI::Base.password = nil
  32. ShopifyAPI::Base.user = nil
  33. end
  34. def teardown
  35. FakeWeb.clean_registry
  36. end
  37. # Custom Assertions
  38. def assert_not(expression)
  39. assert_block("Expected <#{expression}> to be false!") { not expression }
  40. end
  41. def load_fixture(name, format=:json)
  42. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  43. end
  44. def fake(endpoint, options={})
  45. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  46. format = options.delete(:format) || :json
  47. method = options.delete(:method) || :get
  48. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  49. url = if options.has_key?(:url)
  50. options[:url]
  51. else
  52. "http://localhost/admin/#{endpoint}#{extension}"
  53. end
  54. FakeWeb.register_uri(method, url, {:body => body, :status => 200, :content_type => "text/#{format}", :content_length => 1}.merge(options))
  55. end
  56. end