I’ve moved my model specifications (like belongs_to, has_many, validates_ and friends) to RSpec specifications.
This was not very complicated. I admit that it is an early implementation with certain problems.
Please do not use this for anything except experiments. This solutions causes few problems that I haven’t fixed yet
Anyway, let me try to explain what I’ve done.
I rewrote spec/spec_helper.rb:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
ENV["RAILS_ENV"] ||= "test" case ENV['RAILS_ENV'] when 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'controller_mixin' require 'rspec_on_rails' class SpecTestCase < Test::Unit::TestCase self.use_transactional_fixtures = true self.use_instantiated_fixtures = false self.fixture_path = RAILS_ROOT + '/spec/fixtures' # You can set up your global fixtures here, or you # can do it in individual contexts #fixtures :table_a, :table_b def run(*args) end def setup super end def teardown super end end module Spec module Runner class Context def before_context_eval inherit SpecTestCase end end end end Test::Unit.run = true else class << self def context(name,&block) ; end end end |
Then, I’ve created has_specification (I have this script in lib/):
1 2 3 4 5 6 7 |
module ActiveRecord::Specification def has_specification unless ENV['RAILS_ENV'] == 'test' require File.dirname(__FILE__)+"/../spec/models/#{self.to_s.tableize.singularize}_spec" end end end |
and surely added it to ActiveRecord::Base:
ActiveRecord::Base.extend ActiveRecord::Specification |
Now, I was able to move all my specifications to spec/models/model_spec.rb. I did it by just adding something like
1 2 3 |
class Model < ActiveRecord::Base has_many :ideas end |
before specification contexts.
And, finally, I’ve replaced that specifications for app/models/model.rb with has_specification:
1 2 3 |
class Model < ActiveRecord::Base has_specification end |
It seems that it’s all.
I’m sure it could be done much better. But it’s a first quick hack solution to probe the idea, so please don’t beat me.
Was it a good idea at all? :)




