开发者

Rails 3.1 RC5 Mountable Engine testing with Spork

开发者 https://www.devze.com 2023-03-28 12:43 出处:网络
I\'ve been able to get RSpec, Cucumber, and Autotest to work against my Rails 3.1 Mountable Engine.Where things fall down is trying to integrate Spork into the mix.Spork runs fine, and my tests use Sp

I've been able to get RSpec, Cucumber, and Autotest to work against my Rails 3.1 Mountable Engine. Where things fall down is trying to integrate Spork into the mix. Spork runs fine, and my tests use Spo开发者_如何转开发rk, but the problem I'm having is that Spork doesn't reload models unless I bring down the Spork server which isn't exactly efficient. I'm using factory_girl as well. I tried various things using Spork.trap_method, yet nothing has worked.

Here's the gems I'm using (although I have since abandoned Spork in my project due the grief it was causing me):

  • rails (3.1.0.rc5)
  • spork (0.9.0.rc9)
  • rspec (2.6.0)
  • rspec-core (2.6.4)
  • rspec-expectations (2.6.0)
  • rspec-mocks (2.6.0)
  • rspec-rails (2.6.1)
  • factory_girl (2.0.3)
  • factory_girl_rails (1.1.0)
  • cucumber (1.0.2)
  • cucumber-rails (1.0.2)

Thanks,

-Damien


I figured out my problem. It was actually Autotest and not Spork. I've moved from a mountable engine to a standard engine (plugin) since it ended up being a better fit for what I needed.

I'm now using the released version of Rails 3.1.

In this scenario, I figured things would be easier, but I encountered the same issue. Anyway, this ended up being the fix for testing a non-namespaced engine (mountable), although with a few path tweaks, I believe it will work.

Add an .autotest file to the root of the project with the following:

Autotest.add_hook :initialize do |at|
  at.add_mapping %r%^app/models/(.*)\.rb$% do |_, m|
    "spec/models/#{m[1]}_spec.rb"
  end

  at.add_mapping %r%^app/controllers/(.*)\.rb$% do |_, m|
    ["spec/controllers/#{m[1]}_spec.rb",
    "spec/functional/#{m[1]}_spec.rb"]
  end

  at.add_mapping %r%^app/helpers/(.*)_helper.rb% do |_, m|
    ["spec/views/#{m[1]}_view_spec.rb",
    "spec/functional/#{m[1]}_controller_spec.rb"]
  end

  at.add_mapping %r%^app/views/(.*)/% do |_, m|
    ["spec/views/#{m[1]}_view_spec.rb",
    "spec/functional/#{m[1]}_controller_spec.rb"]
  end
end

I came up with the solution when I ran across this answer on another question: how to tell autotest to correctly track changes in app source?, as well as other examples found around the web.

Hope this helps someone else out.

[Edit 2011-09-20] Fixed the Cucumber/Spork problem with a "hack." Within the Spork.each_run block, I forced a reload of the models and controllers like so:

  ENGINE_ROOT=File.join(File.dirname(__FILE__), '../../')
  # Couldn't get spork to reload models, hence the reason for this hack
  Dir[File.join(ENGINE_ROOT, "app/models/*.rb")].each {|f| load f }
  # or controllers...
  Dir[File.join(ENGINE_ROOT, "app/controllers/*.rb")].each {|f| load f }

It seems like there should be a better way...

0

精彩评论

暂无评论...
验证码 换一张
取 消