Мое приложение имеет контроллер по умолчанию, выделенный в драгоценный камень (gem 'abstracted'), и все мои контроллеры наследуют от этого абстрактного контроллера.как я могу протестировать AccountsController <AbstractedResourcesController
# Gemfile
gem 'abstracted', path: '../../gems/abstracted'
# app/controllers/accounts_controller.rb
class AccountsController < AbstractResourcesController
end
Теперь, когда я пишу контроллер (мини) тесты для одного из этих контроллеров, как это:
require "test_helper"
describe AccountsController do
...
Я получаю эту ошибку один раз ударять: тест грабли из командной строки:
NameError: uninitialized constant AbstractResourcesController
/path_to_rails/projects/cas_server/app/controllers/accounts_controller.rb:1:in `<top (required)>'
Мой тест/test_helper.rb выглядит следующим образом:
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/autorun'
require 'action_controller/test_case'
require 'capybara/rails'
require 'rails/test_help'
require 'minitest-rails'
require 'minitest/pride'
# require 'miniskirt'
# require 'factories'
# require 'mocha'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionController::TestCase
include Devise::TestHelpers
end
class MiniTest::Unit::TestCase
# include MiniTest::ActiveRecordAssertions
# DatabaseCleaner.strategy = :transaction
#
# def setup
# DatabaseCleaner.start
# end
#
# def teardown
# DatabaseCleaner.clean
# end
end
class MiniTest::Spec
include ActiveSupport::Testing::SetupAndTeardown
# alias :method_name :__name__ if defined? :__name__
def build_message(*args)
args[1].gsub(/\?/, '%s') % args[2..-1]
end
end
class ControllerSpec < MiniTest::Spec
include ActionController::TestCase::Behavior
include Devise::TestHelpers
include Rails.application.routes.url_helpers
# Rails 3.2 determines the controller class by matching class names that end in Test
# This overides the #determine_default_controller_class method to allow you use Controller
# class names in your describe argument
# cf: https://github.com/rawongithub/minitest-rails/blob/gemspec/lib/minitest/rails/controller.rb
def self.determine_default_controller_class(name)
if name.match(/.*(?:^|::)(\w+Controller)/)
$1.safe_constantize
else
super(name)
end
end
before do
@controller = self.class.name.match(/((.*)Controller)/)[1].constantize.new
@routes = Rails.application.routes
end
subject do
@controller
end
end
# Functional tests = describe ***Controller
MiniTest::Spec.register_spec_type(/Controller$/, ControllerSpec)
Nope - это уже там :( –
обновил мой вопрос, чтобы показать test_helper.rb –
@walt_die я обновил свой ответ – rick