Ver código fonte

Make sure Session initialization fails, when no shop is provided in constructor. Added test for that. Also added tobi's context ultralite remix ;)

Dennis Theisen 15 anos atrás
pai
commit
c7bbe5af1a
3 arquivos alterados com 24 adições e 12 exclusões
  1. 1 0
      lib/shopify_api.rb
  2. 15 12
      test/shopify_api_test.rb
  3. 8 0
      test/test_helper.rb

+ 1 - 0
lib/shopify_api.rb

@@ -97,6 +97,7 @@ module ShopifyAPI
     end
 
     def initialize(url, token = nil)
+      raise ArgumentError.new("You must provide at least a URL to a Shopify store!") if url.blank?
       url.gsub!(/https?:\/\//, '')                            # remove http:// or https://
       url = "#{url}.myshopify.com" unless url.include?('.')   # extend url to myshopify.com if no host is given
       

+ 15 - 12
test/shopify_api_test.rb

@@ -1,18 +1,21 @@
 require 'test_helper'
 
 class ShopifyApiTest < Test::Unit::TestCase
-  def setup
-    @session_no_token = ShopifyAPI::Session.new("testshop.myshopify.com")
-    @session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
-  end
-  
-  
-  def test_session_not_valid_without_token
-    assert_not @session_no_token.valid?
-  end
   
-  def test_session_is_valid_with_any_token
-    assert @session.valid?
+  context "Session" do
+    test "should raise error when blank shop url is provided" do
+      assert_raise(ArgumentError) { ShopifyAPI::Session.new("") }
+    end
+
+    test "should not be valid without token" do
+      session = ShopifyAPI::Session.new("testshop.myshopify.com")
+      assert_not session.valid?
+    end
+
+    test "should be valid with any token" do
+      session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
+      assert session.valid?
+    end
+    
   end
-  
 end

+ 8 - 0
test/test_helper.rb

@@ -9,6 +9,14 @@ require 'shopify_api'
 ShopifyAPI::Session.setup(:api_key => "API Test key", :secret => "API Test secret")
 
 class Test::Unit::TestCase
+  def self.test(string, &block)
+    define_method("test:#{string}", &block)
+  end
+  
+  def self.context(string)
+    yield
+  end
+  
   # Custom Assertions
   def assert_not(expression)
     assert_block("Expected <#{expression}> to be false!") { not expression }