Trying to fix NullDB for Rails3/RSpec2.5.1

I’m in the midst of upgrading an app from Rails 2.3 to Rails 3, and as
part of the process, from RSpec to RSpec2.
One thing that seems to have broken for me is the use of NullDB to
nullify the database contextually for a single spec.

I think I’ve narrowed it down to this method in NullDB which always
seems to return false, no matter where I put the line:
include NullDB::RSpec::NullifiedDatabase

Can someone either sanity-check me here, or provide a different way of
knowing when you are inside a describe block?

def self.nullify_contextually?(other)
rspec_root = defined?(RSpec) ? RSpec : Spec
if defined? rspec_root::Rails::RailsExampleGroup
other.ancestors.include?(rspec_root::Rails::RailsExampleGroup)
else
other.ancestors.include?(rspec_root::Rails::ModelExampleGroup) ||
other.ancestors.include?(rspec_root::Rails::ControllerExampleGroup)
||
other.ancestors.include?(rspec_root::Rails::ViewExampleGroup) ||
other.ancestors.include?(rspec_root::Rails::HelperExampleGroup)
end
end

Thanks,
Matt Van Horn

On Apr 23, 2011, at 10:40 AM, Matthew Van Horn wrote:

end
Changing ‘ancestors’ to ‘included_modules’ seems to make this work for
me.
ancestors is returning a one-element array with an anonymous class in
it.
included_modules is returning what I’d expect
[NullDB::RSpec::NullifiedDatabase, RSpec::Rails::ModelExampleGroup,
RSpec::Rails::RailsExampleGroup… etc]
Could this be a ruby version thing?
I’m using ree 1.8.7 for the moment.

– matt

On Apr 23, 2011, at 10:26 AM, Matthew Van Horn wrote:

    other.ancestors.include?(rspec_root::Rails::HelperExampleGroup)
end

end

Changing ‘ancestors’ to ‘included_modules’ seems to make this work for me.
ancestors is returning a one-element array with an anonymous class in it.
included_modules is returning what I’d expect [NullDB::RSpec::NullifiedDatabase,
RSpec::Rails::ModelExampleGroup, RSpec::Rails::RailsExampleGroup… etc]
Could this be a ruby version thing?
I’m using ree 1.8.7 for the moment.

More likely an execution order issue than a Ruby version issue. RSpec
tells each example group to include the modules listed in
included_modules. If nullify_contextually? is invoked before that
happens, the modules will be in the included_modules Array, but will be
included yet, so ancestors would not return them. Make sense?

On Apr 23, 2011, at 11:45 AM, David C. wrote:

    other.ancestors.include?(rspec_root::Rails::ControllerExampleGroup) ||

More likely an execution order issue than a Ruby version issue. RSpec tells each
example group to include the modules listed in included_modules. If
nullify_contextually? is invoked before that happens, the modules will be in the
included_modules Array, but will be included yet, so ancestors would not return
them. Make sense?

Probably makes more sense to Myron, but I understand what you’re saying.
– matt