-
-
Save coreyhaines/2068977 to your computer and use it in GitHub Desktop.
--colour | |
-I app |
require 'active_record' | |
connection_info = YAML.load_file("config/database.yml")["test"] | |
ActiveRecord::Base.establish_connection(connection_info) | |
RSpec.configure do |config| | |
config.around do |example| | |
ActiveRecord::Base.transaction do | |
example.run | |
raise ActiveRecord::Rollback | |
end | |
end | |
end |
class Coderetreat < ActiveRecord::Base | |
def self.running_today | |
where(scheduled_on: Date.today) | |
end | |
end |
require 'active_record_spec_helper' | |
require 'models/coderetreat' | |
describe Coderetreat do | |
describe ".running_today" do | |
it "returns a coderetreat scheduled for today" do | |
coderetreat = Coderetreat.create! city: "Chicago", scheduled_on: Date.today | |
Coderetreat.running_today.all.should =~ [coderetreat] | |
end | |
it "does not return a coderetreat not scheduled for today" do | |
coderetreat = Coderetreat.create! city: "Chicago", scheduled_on: Date.today.advance(:days => -1) | |
Coderetreat.running_today.should be_empty | |
end | |
end | |
end |
#Some databases get upset if you try to start a new transaction while a transaction is already in play, so running the whole spec suite chokes when rspec is trying to start a transaction. You need to update your spec_helper to rely on active_record_spec_helper to do this for you. | |
#Replace this line | |
config.use_transactional_fixtures = true | |
#With this | |
require 'active_record_spec_helper' | |
#TADA |
I love this idea, and I really want to implement it more in my test suites. But I'm having the problem that my ActiveRecord models just have too damn many dependencies, and I have to add so many "requires" at the top of my spec file that I might as well just require spec_helper
.
Of course, the obvious solution here is to reduce the number of dependencies. And in some cases this is pretty easy, but it's often not, because a lot of the dependencies are due to my associations.
E.g. say I have an model User
that looks like this:
class Comment < ActiveRecord::Base
belongs_to :design
end
If I set up my spec like this:
require 'active_record_spec_helper'
require "comment"
describe Comment do
... whatever
end
... then it will fail, because when it tries to load "comment.rb", it hits the line belongs_to :design
, and starts looking for a Design
class - which hasn't been required, so the whole thing crashes with uninitialized constant Comment::Design
.
So I add require 'design'
to the top of my spec - but within my Design
class I have the line belongs_to :user
, which crashes with uninitialized constant Design::User
... so I add require 'user'
to the top of my spec, which hits the line has_many :posts
and raises uninitialized constant User::Post
... and so on until I've required almost my entire app/models
directory.
Is there something conceptual I'm missing? How do I get around this? Basic Rails associations don't seem like something I should be refactoring out into a separate class or module, and stubbing out the associations feels a bit icky too (plus I'm not entirely sure how to do it anyway.)
Anyone have any pointers?
Sorry, me again. StackOverflow bounty is available to anyone who can help me out with the above question:
Did anyone came up with a solution to the question of georgemillo? I also struggle with the idea of loading all dependent models, which is undesireable to me.
I had to add
require "yaml"
to the top ofactive_record_spec_helper
to get this to work. RSpec 3.1.7, Rails 4.1.8.