Giter VIP home page Giter VIP logo

shopify-mock's Introduction

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 test
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 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.

shopify-mock's People

Contributors

travishaynes avatar alexcp avatar bunnymatic avatar hjblok avatar skwp avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.