My suggestion is to use anonymous classes declared in-line:
# ./spec/foo_spec.rb
require 'spec_helper'
describe 'Foo' do
let(:person_class) do
Class.new(ActiveRecord::Base) do
has_many :abilities
end
end
it 'has relations' do
expect(person_class.reflections.keys).to eq %w(abilities)
end
end
# ./spec/bar_spec.rb
require 'spec_helper'
describe 'Bar' do
let(:person_class) do
Class.new(ActiveRecord::Base) do
has_many :children
end
end
it 'has relations' do
expect(person_class.reflections.keys).to eq %w(children)
end
end
There are two nice things about this:
Class.new
always makes a new class.The downside is that the class does not have a constant, which might not play nice with activerecord (it may not support anonymous subclasses) and it will be less pretty in error messages.
If you want to make it a named class, you can use RSpec’s constant stubbing; it exists specifically for this:
# ./spec/foo_spec.rb
require 'spec_helper'
describe 'Foo' do
before do
stub_const("Person", Class.new(ActiveRecord::Base) do
has_many :abilities
end)
end
it 'has relations' do
expect(Person.reflections.keys).to eq %w(abilities)
end
end
# ./spec/bar_spec.rb
require 'spec_helper'
describe 'Bar' do
before do
stub_const("Person", Class.new(ActiveRecord::Base) do
has_many :children
end)
end
it 'has relations' do
expect(Person.reflections.keys).to eq %w(children)
end
end
RSpec will take care of cleaning up the Person
constant created in each example, ensuring the constants do not leak and cause problems.
HTH,
Myron
--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/8e469a6b-1175-4299-bf99-c90ffd587ca6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.