Matthew Van Horn
2011-Apr-23 14:40 UTC
[rspec-users] 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110423/5239b6fb/attachment.html>
Matthew Van Horn
2011-Apr-23 15:26 UTC
[rspec-users] Trying to fix NullDB for Rails3/RSpec2.5.1
On Apr 23, 2011, at 10:40 AM, Matthew Van Horn wrote:> 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 > endChanging ''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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110423/506f27de/attachment.html>
David Chelimsky
2011-Apr-23 15:45 UTC
[rspec-users] Trying to fix NullDB for Rails3/RSpec2.5.1
On Apr 23, 2011, at 10:26 AM, Matthew Van Horn wrote:> On Apr 23, 2011, at 10:40 AM, Matthew Van Horn wrote: > >> 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 > > 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? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110423/15b4273d/attachment.html>
Matthew Van Horn
2011-Apr-23 16:27 UTC
[rspec-users] Trying to fix NullDB for Rails3/RSpec2.5.1
On Apr 23, 2011, at 11:45 AM, David Chelimsky wrote:> On Apr 23, 2011, at 10:26 AM, Matthew Van Horn wrote: > >> On Apr 23, 2011, at 10:40 AM, Matthew Van Horn wrote: >> >>> 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 >> >> 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?Probably makes more sense to Myron, but I understand what you''re saying. -- matt -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110423/c05a59fe/attachment.html>