Skip to content

Instantly share code, notes, and snippets.

@alex-sysoev
Last active October 12, 2017 19:51
Show Gist options
  • Save alex-sysoev/3fc29817f51d14a82a3d to your computer and use it in GitHub Desktop.
Save alex-sysoev/3fc29817f51d14a82a3d to your computer and use it in GitHub Desktop.
Spree application specs based on native factories.

Testing Spree with RSpec

Spree has a lot of native factories, that could help you to test your spree-based application, creating "testing infrastructure". The first thing you should do (if it is not done yet) is to load Spree factories in your app.

#spec_helper.rb
  require 'spree/testing_support/factories'

  #As well you might want to add more spree testing stuff such as preferences:
  require 'spree/testing_support/preferences'
  
  #One more thing we should add here- Devise test helpers to simulate user authentication
  RSpec.configure do |config|
    ...
    config.include Devise::TestHelpers, type: :controller
    ...
  end
  

Factories

Often you add some new attributes to existing Spree:: or modify/add it's validations. In this case the valid Spree factory for this model could become invalid. Lets try to make new factory based on the old one.

#spree_users.rb adding new attribute 'rut'
  FactoryGirl.define do
    factory :user_with_rut, parent: :user do
      rut '11.111.111-1'
    end
  end
  
#spree_addresses.rb suddenly we've changed the phone format for our Spree::Address model and old factory became invalid 
  FactoryGirl.define do
    factory :address_new_format, parent: :address do
      phone '64837921'
      alternative_phone '64837921'
    end
  end

#spree_orders.rb and make our Spree::Order model to deal with new factories
  FactoryGirl.define do
    factory :order_new, parent: :order do
      association :user, factory: :user_with_rut
      association :bill_address, factory: :address_new
      association :ship_address, factory: :address_new
      completed_at nil
      email { user.email }
      order_rut '11.111.111-1' #new one
      seller_rut '28.556.792-0' #new one
    end
  end
  

Controller specs

In some cases we need to have a logged in user to be able to test some controller method Use Devise helpers

#some_controller_spec.rb
  ...
  
  describe 'some action' do
  	before(:each) do
  		#integrating spree routes if necessary
  		@routes = Spree::Core::Engine.routes
  
  		#sign in user
  		@user 	= FactoryGirl.create(:user_with_rut)
  		sign_in @user
  	end
  end
	
  ...

If your application is a bit more complicated that you use a gem 'multi_domain' I found useful to stub current store in controller

#some_controller_spec.rb

  ...
  
  describe 'some action' do 
    #setting current store
  	store   = FactoryGirl.create(:store)
  	controller.stub(:current_store => store)
  end
  
  ....

Some day you might wish to update the spree version on your application than you'd better have a strong net of specs, to be sure that new version code won't break your's one.

Take care

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment