ShopifyMock

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, ShopifyMock is disabled by default, and real-world Internet access is allowed. In a Rails app, ShopifyMock is disabled except for in the :test environment.

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

ShopifyAPI::Mock.enabled = true # or false

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

ShopifyAPI::Mock.allow_internet = false # or true to re-enable

Example

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

rails c
:001 > ShopifyAPI::Mock.enabled = true
:002 > order = ShopifyAPI::Session.temp("test", "randomtoken") { ShopifyAPI::Order.first }

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

Fixtures

You have access to the ShopifyMock fixtures from within your app to help with testing. To access a fixture, use ShopifyAPI::Mock::Fixtures:

ShopifyAPI::Mock::Fixtures.read(:orders) # => contents of orders.json

You can also use your own fixtures if you’d like:

ShopifyAPI::Mock::Fixtures.use :orders, order.to_json # => use custom content for :orders
ShopifyAPI::Mock::Fixtures.use :orders, :default      # => reset :orders to default fixture
ShopifyAPI::Mock::Fixtures.reset                      # => reset all the fixtures back to their defaults

Or, if you’d prefer you can point the entire fixture path to a different location:

ShopifyAPI::Mock::Fixtures.path = File.join(Rails.root, 'spec', 'fixtures')

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

Copyright © 2011 Travis Haynes. See LICENSE.txt for further details.