I have a module that defines a class method to dynamically define a series of instance methods based on values in given columns, roughly as follows:
lib/active_record_extension.rb
module ActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
def define_some_methods(*attribute_names)
# define some methods
end
end
end
ActiveRecord::Base.send(:include, ActiveRecordExtension)
config/initializers/extensions.rb
require 'active_record_extension.rb'
app/models/my_model.rb
class MyModel < ActiveRecord::Base
define_some_methods :first_attribute, :second_attribute
end
This setup for adding a class method to ActiveRecord::Base is based on the first answer to this question.
This works beautifully in my Rails app and console, allowing me to define a variety of similar methods without cluttering up my model. However, it doesn't work at all in my rspec tests, which now all fail with NoMethodError
s for calls to the dynamically defined methods.
How can I be sure this module (or just this method) is correctly included in my models while running rspec?
EDIT: Here is my spec/spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
#to test with sunspot
require 'sunspot/rails/spec_helper'
RSpec.configure do |config|
::Sunspot.session = ::Sunspot::Rails::StubSessionProxy.new(::Sunspot.session)
end
#adds devise and jasmine fixture test helpers
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
end