/spec/controllers/spree/api/custom_products_controller.rbbundle exec rspec spec/controllers/spree/api/custom_products_controller.rb
Failure/Error: get :index ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"spree/api/custom_products"}require 'rails_helper'
module Spreedescribe Spree::Api::CustomProductsController, type: :controller do let(:shipping_category) { Spree::ShippingCategory.create!(name: 'default') }
before do Spree::Customization.create!(reference: '01', price: 10) Spree::Customization.create!(reference: '02', price: 20) end
describe 'GET #index' do
context 'with existing custom products' do before do Spree::Product.create!(name: 'Product1', price: 5, shipping_category_id: shipping_category.id) # normal product Spree::Product.create!(name: 'CustomProduct1', price: 10, shipping_category_id: shipping_category.id, perso_id: 1) # custom products Spree::Product.create!(name: 'CustomProduct2', price: 20, shipping_category_id: shipping_category.id, perso_id: 2) api_get :index end
it 'responds with 200' do expect(response).to be_success expect(response).to have_http_status(200) end
it 'return 2 resources' do resources = JSON.parse(response.body) expect(resources.length).to eq(2) end endend# spec/rails_helper.rb
require 'spree/api/support/controller_hacks'
RSpec.configure do |config|
[...] config.include Spree::API::Support::ControllerHacks, :type => :controllerend/Library/Ruby/Gems/2.0.0/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require': cannot load such file -- spree/api/support/controller_hacks (LoadError)
require File.join(Rails.root, 'spree/api/support/controller_hacks')
stub_authentication!https://github.com/spree/spree/blob/2-3-stable/api/spec/controllers/spree/api/products_controller_spec.rb
module Spree
...
before do
stub_authentication!
end
...
endFailure/Error: expect(response).to be_success expected `#<ActionController::TestResponse:0x007fb4113fc4e8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fb41176b930>, @stream=#<ActionDispatch::Response::Buffer:0x007fb4114b1668 @response=#<ActionController::TestResponse:0x007fb4113fc4e8 ...>, @buf=["{\"exception\":\"undefined method `authenticate' for nil:NilClass\"}"],require 'spree/api/testing_support/helpers'
RSpec.configure do |config|
config.include Spree::Api::TestingSupport::Helpers, :type => :controller
end
The testing support helpers are already on your relative path, so you can just require them with the line above.
Martin
# /spec/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'ENV["RAILS_ENV"] ||= 'test'require 'spec_helper'require File.expand_path("../../config/environment", __FILE__)require 'rspec/rails'
require '/Users/Swibge/.bundler/ruby/2.0.0/spree-b6fba810ec20/api/spec/support/controller_hacks'require 'spree/api/testing_support/helpers'require 'spree/api/testing_support/setup'
# Requires supporting ruby files with custom matchers and macros, etc, in# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are# run as spec files by default. This means that files in spec/support that end# in _spec.rb will both be required and run as specs, causing the specs to be# run twice. It is recommended that you do not name files matching this glob to# end with _spec.rb. You can configure this pattern with with the --pattern# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.# If you are not using ActiveRecord, you can remove this line.ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = true
# RSpec Rails can automatically mix in different behaviours to your tests # based on their file location, for example enabling you to call `get` and # `post` in specs under `spec/controllers`. # # You can disable this behaviour by removing the line below, and instead # explicitly tag your specs with their type, e.g.: # # RSpec.describe UsersController, :type => :controller do # # ... # end # # The different available types are documented in the features, such as in config.infer_spec_type_from_file_location!
config.include ControllerHacks, :type => :controller config.include Spree::Api::TestingSupport::Helpers, :type => :controller config.extend Spree::Api::TestingSupport::Setup, :type => :controllerend
group :test do gem 'factory_girl' gem 'capybara' gem 'faker'end/spec/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'ENV["RAILS_ENV"] ||= 'test'require 'spec_helper'require File.expand_path("../../config/environment", __FILE__)require 'rspec/rails'
# == Load core factories and other testing support libreries from Spree corerequire 'spree/testing_support/factories'require 'spree/testing_support/preferences'
# == Load API helpersrequire 'spree/api/testing_support/helpers'require 'spree/api/testing_support/setup'
# == Load Controller hacks helper (api_get, api_post...)require '/Users/Swibge/.bundler/ruby/2.0.0/spree-b6fba810ec20/api/spec/support/controller_hacks' config.include FactoryGirl::Syntax::Methods config.include Spree::Api::TestingSupport::Helpers, :type => :controller config.extend Spree::Api::TestingSupport::Setup, :type => :controller config.include Spree::TestingSupport::Preferences, :type => :controller config.include Devise::TestHelpers, :type => :controller config.extend ControllerMacros, :type => :controllerend
before do
stub_authentication!
end#spec/support/controller_macros.rb
module ControllerMacros
def login_admin
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryGirl.create(:admin_user) # Using factory girl as an example
end
end
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user)
user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
sign_in user
end
end
end
stub_authentication! is only stubbing authentication for API requests. You cannot use it to login to the backend or frontend of Spree. For that you will need the Controller Macros that you have found.