test_helper.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require 'rubygems'
  2. require 'test/unit'
  3. require 'fakeweb'
  4. require 'mocha'
  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.site = "http://localhost/admin"
  30. ShopifyAPI::Base.password = nil
  31. ShopifyAPI::Base.user = nil
  32. end
  33. def teardown
  34. FakeWeb.clean_registry
  35. end
  36. # Custom Assertions
  37. def assert_not(expression)
  38. assert_block("Expected <#{expression}> to be false!") { not expression }
  39. end
  40. def load_fixture(name, format=:json)
  41. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  42. end
  43. def fake(endpoint, options={})
  44. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  45. format = options.delete(:format) || :json
  46. method = options.delete(:method) || :get
  47. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  48. url = "http://localhost/admin/#{endpoint}#{extension}"
  49. FakeWeb.register_uri(method, url, {:body => body, :status => 200, :content_type => "text/#{format}", :content_length => 1}.merge(options))
  50. end
  51. end