Skip to content

Serves Shopify resources via FakeWeb for easily testing Shopify apps.

Notifications You must be signed in to change notification settings

travishaynes/shopify-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shopify-mock

This gem is used for testing Shopify apps without having to actually connect to Shopify to develop the application.

You can use this gem explicitly for testing, or you can also use it in your development environment to speed things up when fiddling around in the web browser, or in the console.

Installation

Add the gem to your Gemfile in the appropriate group:

gem 'shopify-mock', :group => [:development, :test]

Enabling / Disabling

For non Rails apps, shopify-mock is disabled by default, and real-world Internet access is allowed. In a Rails app, shopify-mock is disabled except for in the :test environment.

To enable / disable shopify-mock manually, set ShopifyAPI::Mock.enabled to true or false:

ShopifyAPI::Mock.enabled = true         # or false to disable
ShopifyAPI::Mock.enabled?               # => to get the current state

And to completely disable access to the Internet altogether, or re-enable it:

ShopifyAPI::Mock.allow_internet = false # or true to re-enable
ShopifyAPI::Mock.allow_internet?        # => to get the current state

Example

After installing the gem in your Shopify app, load the rails console, and try this quick example:

rails c -e test

ShopifyAPI::Base.api_version = '2019-04'
session = ShopifyAPI::Session.new(domain: "domain", token: "randomtoken", api_version: "2019-04")
ShopifyAPI::Base.activate_session(session)
ShopifyAPI::Order.first

You’ll notice that the order was not downloaded from Shopify, but based off of a shopify-mock fixture found in lib/shopify-mock/fixtures/orders.json

Responses

You have access to all the registered mock responses through ShopifyAPI::Mock::Response

ShopifyAPI::Mock::Response.all   # => array of all registered responses
ShopifyAPI::Mock::Response.clear # => clears all the currently registered responses

You also have access to each Shopify object asset by it’s ‘id` based on the ids in the fixtures. If you are using the fixtures include in the gem, you can do

product = ShopifyAPI::Product.find(632910392)

and you’ll get back the fixture product called ‘Ipod Nano 8GB`

And you can register your own response:

ShopifyAPI::Mock::Response.new(:get, "orders/1.xml", "response content")

Fixtures

You have access to the shopify-mock fixtures through ShopifyAPI::Mock::Fixture

ShopifyAPI::Mock::Fixture.all                   # => an array of all the fixtures
ShopifyAPI::Mock::Fixture.find(:orders, :json)  # => returns the orders.json fixture
ShopifyAPI::Mock::Fixture.path                  # => the path to the fixture files

# to use your own fixture path:
ShopifyAPI::Mock::Fixture.path = File.join(Rails.root, 'spec', 'fixtures')
ShopifyAPI::Mock.reset # => to reload the fixtures

A Fixture object has these methods:

fixture = ShopifyAPI::Mock::Fixture.all.first

fixture.name  # => the name of the fixture, such as :orders
fixture.ext   # => the extension of the fixture, such as :json, or :xml
fixture.data  # => the file contents

# to override the fixture's contents:
fixture.data = "new contents" # => use "new contents" for this fixture
fixture.data = nil            # => resets back to default contents

Contributing to shopify-mock

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Resources

  • All of the default fixtures were copied directly from the Shopify API

License

MIT License

Copyright © 2011 Travis Haynes.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Serves Shopify resources via FakeWeb for easily testing Shopify apps.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages