Hello,
It’s been a while when I last updated this blog. It seems that Rspec 2 can not use fixtures from Activerecord 2.3.x !
Impossible ? Not really, and thanks to ruby it was possible for me.
Let’s describe the background :
First of all, I had a gem built on jeweler that uses ActiveSupport::TestCase and ActiveRecord::Fixtures exactly implemented like in Rails
But one day, I decided to switch to RSpec (jeweler (1.5.2) uses as of my writing rspec ~> 2.3.0)
The conversion of test_* to “it” syntax was very quick and easy but loading fixtures was not !!!
So here is my implementation if you want to know how can this be possible :
# spec/spec_helper.rb # .... some stuff ... require 'activerecord' module MyExtensions def self.extended(base) class << base # Beginning of Proxy def subclass_with_fixtures(*args,&block) child = subclass_without_fixtures(*args,&block) child.module_eval do include ActiveSupport::Callbacks define_callbacks :setup, :teardown include ActiveRecord::TestFixtures self.fixture_path = File.expand_path("../db/fixtures",__FILE__) self.use_instantiated_fixtures = false self.use_transactional_fixtures = true self.set_fixture_class :categories => 'Categorie' def run_in_transaction? true end end child end # End of method chain subclass_with_fixtures alias_method_chain :subclass, :fixtures end # End of Proxy end # End of extended end # End of MyExtensions RSpec::Core::ExampleGroup.extend(MyExtensions) RSpec.configure do |config| config.after(:each) do run_callbacks :teardown, :enumerator => :reverse_each end config.before(:each) do run_callbacks :setup end end
In my fixtures :
# spec/db/fixtures/posts.yml
a_post:
title: "My first post"Now when I want to load fixtures :
# spec/post_spec.rb describe "Post" do before(:all) do self.class.fixtures :posts end it "should not save without a title" do post = posts(:a_post) post.title = nil post.save.should_not be_true end end
Maybe, a better way can be found … if so please inform me !!!

