Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting and integrating plain ruby objects into a Rails 3.2 application
    primarykey
    data
    text
    <p>After watching the <a href="http://www.confreaks.com/videos/641-gogaruco2011-fast-rails-tests" rel="nofollow">Fasts Rails Tests</a> talk by Corey and reading through <a href="http://devblog.avdi.org/2011/11/15/early-access-beta-of-objects-on-rails-now-available-2/" rel="nofollow">Object on Rails</a> by Avdi I am in the process of setting up my new Rails 3.2 application to take advantage of these concepts.</p> <p>In order to get everything working I started with this example spec file.</p> <pre><code># spec_no_rails/projects/financials_spec.rb require_relative '../spec_no_rails_helper' class DummyProject include Modules::Projects::Financials end describe Modules::Projects::Financials do it 'should have some method' do DummyProject.new.foo.should == 'bar' end end </code></pre> <p>And this was the intialial spec_no_rails_helper.rb file that is used to require the modules</p> <pre><code># spec_no_rails/spec_no_rails_helper.rb Dir["#{Dir.pwd}/app/pimms/**/*.rb"].each { |file| require file } </code></pre> <p>I then set about creating the new example module.</p> <pre><code># app/pimms/modules/projects/financials.rb module Modules::Projects::Financials def foo 'bar' end end </code></pre> <p>In order to see that everything was going to work when I included the new stand alone module into one of my ActiveRecord classes I added the following line into one of my models.</p> <pre><code># app/models/project.rb class Project &lt; ActiveRecord::Base include Modules::Projects::Financials end </code></pre> <p>This allowed me to open up console and see that everything is working as expected.</p> <pre><code>&gt; Project.first.foo =&gt; "bar" </code></pre> <p>So at this stage I have defined a namespaced stand alone module defined under app/pimms/modules/projects/financials.rb which I can included into a Rails model and everything works as expected.</p> <p>The problem I'm having is when I try to run the specs I get the following.</p> <pre><code>&gt; bundle exec rspec spec_no_rails/ /Users/scott/Code/pimms/spec_no_rails/projects/financials_spec.rb:5:in `&lt;class:DummyProject&gt;': uninitialized constant DummyProject::Modules (NameError) from /Users/scott/Code/pimms/spec_no_rails/projects/financials_spec.rb:4:in `&lt;top (required)&gt;' from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load' from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `block in load_spec_files' from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `map' from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load_spec_files' from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in `run' from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process' from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run' from /Users/scott/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun' </code></pre> <p>So the reason this happens is because the Modules::Projects namespaces has not been defined when the tests are being run. I'm guesing I didn't have to define the Modules::Projects namespace when I used the module with the Rails application because Rails handled that for me.</p> <p>In order to get the test to run as expected I had to define the namespace in the spec_no_rails_helper.rb file like this.</p> <pre><code># spec_no_rails/spec_no_rails_helper.rb module Modules module Projects end end Dir["#{Dir.pwd}/app/pimms/**/*.rb"].each { |file| require file } </code></pre> <p>This obviously isn't ideal as I would have to manually create all the namespaces for any stand alone modules or classes that I wasnt to test outside of Rails.</p> <p>Is there a better way to setup my Rails application so that I can easily run a test suite without relying on Rails?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload