Load up the lib files when running Test::Unit tests in Rails? - ruby-on-rails

I've added a few modules and dropped them in my /lib directory and I think the lib directory is loaded magically by Rails (unless I loaded the lib directory somewhere early in my project and forgot about it). However, when I run unit tests that require my additional modules, they are not loaded.
Should the lib directory be loaded automatically when running tests, or is there an elegant way to do so for testing? I had hoped that the rake scripts + Test::Unit would've loaded up my Rails environment exactly, but this doesn't seem to be the case. I'm left with doing adding something like this to test_helper.rb:
require File.expand_path(File.dirname(__FILE__) + "/../lib/foo")
I'm running my tests with the standard rake scripts like:
rake test
rake test:units
rake test:functionals

Your lib directory is not automatically loaded by rails. Loading occurs through ActiveSupport::Dependencies overriding const_missing. When you use a constant for the first time, if it is undefined, Rails attempts to find it in the lib directory (and other places in the load path). To accomplish this, it uses a naming scheme where something called SomeClass is expected to be in some_class.rb. Rails in test mode uses the same mechanism. Check your config/environments/test.rb and config/environments/development.rb to see if you do something funny with requires. In short, check your naming scheme.

Related

Create rake task to compile SASS file that #import's from gem

I'd like to create a rake task compiles some SASS files that refer to #import's in gems to speed up my development workflow.
Some background
We've recently added a new theme to our rails application, and the sass stylesheets really bog down the assset pipeline in development environment (to the extend of frequently causing timeouts)
The theme relies heavily on imports and mixins which would be very tedious to try and manually convert into files that we could just *= require as some guides suggest.
The files are relatively static, so there's very little reason they should be compiled on every request BUT I have other assets that are worked on frequently so I'd prefer not to rely on rake assets:precompile)
All of the themes are included by a single file (theme/base.scss) that #imports all the rest, so I was thinking an easy solution would be to take that file out of application.css and create a rake task theme:precompile that compiles it to theme.css which is then required by application.css
That way if we do need to tweak the theme file, we can update it and run the rake task, and then the asset pipeline just has a simple static css file to work with.
At first, I tried setting up a rake file that simply invokes sass
desc 'Precompile the theme SASS to save on development time'
task :precompile do |t, args|
dir = 'app/assets/stylesheets/'
source = 'theme/base.scss'
dest = 'theme.css'
source_path = dir + src
dest_path = dir + dst
puts "Precomipling #{source_path} to #{dest} ..."
`sass "#{source_path}" "#{dest_path}"`
end
but that failed because the theme relies on the bootstrap-sass gem, giving
Error: File to import not found or unreadable: bootstrap-sprockets.
So it seems my rake task needs to load the whole sprockets environment, or otherwise figure out how to tell SASS where the bootstrap-sass gem is.
Is this possible? Or is there a better solution?
I didn't find an answer to the question I asked.
In the end, my workaround was that I pulled the #imports into a separate file that stayed in the asset pipeline and used the raketask in my question.

How to require fixture_builder in cucumber?

I have a Rails 4.1 project that uses RSpec and Cucumber.
I've recently added fixture_builder.
fixture_builder.rb includes logic to rebuild fixtures any time the file changes.
This works fine for RSpec with require 'fixture_builder' in spec_helper.rb.
However, when running Cucumber tests fixture_builder.rb is not called, so fixtures are not updated if any changes have been made to fixture_builder.rb.
Is there an equivalent config file like spec_helper.rb for Cucumber?
By default, Cucumber loads all *.rb files in the same directory as the feature(s) it's running and all subdirectories of that directory, so you can put your require in any file you want in any of those directories.
The conventional thing to do is to put 'support' code like require in features/support/env.rb, or in another file in features/support if your env.rb gets too big.
The cucumber-rails gem provides a generator that sets up Cucumber to work with Rails. If you haven't already, install the gem and run
rails g cucumber:install
to create features/support/env.rb and the rest of the usual Rails + Cucumber setup.

Rake assets are generated twice when precompiling them once from command line and once from within a Ruby script

When I do $ rake assets:precompile from the command line twice, then the assets are only generated once (as expected).
When I execute system 'rake assets:precompile' twice within a Ruby file, then the assets are also only generated once (as expected).
But when I execute it once from the command line and once from the Ruby file, then the assets are generated twice!
application-3e5c3563c16dfbc76bb833355133818d.js
application-3e5c3563c16dfbc76bb833355133818d.js.gz
application-61c5fbc18ff978365b43849b5787130e.css
application-61c5fbc18ff978365b43849b5787130e.css.gz
application-bb95b4b82324f57eb5d4ef0d20d5a68d.js
application-bb95b4b82324f57eb5d4ef0d20d5a68d.js.gz
application-c17c2cec3bacccb8ef58471e949679f2.css
application-c17c2cec3bacccb8ef58471e949679f2.css.gz
manifest-1627575ea003fb94ae02acecd73aecef.json
Why's that? And how can it be prevented? Thank you.
The reason why I'm doing this:
I'm searching for unused CSS selectors throughout my application. I forked the old DeadWeight gem and updated it so it's working with newer versions of Rails.
The problem is that I need the fully compiled application.css file when initializing DeadWeight, so I precompile it from within DeadWeight. And there I noticed that when there's already a precompiled version that was precompiled from the console (and not from within DeadWeight), it's not overwritten! So I have to manually remove it first (by doing another rake assets:clobber/clean) which isn't nice.

How can I configure RSpec to run my model tests?

No matter what I do, the only tests that run with the rake test command are those in spec/requests. Naturally, I would like to run everything in the spec directory.
I thought getting the gem and installing RSpec would do it, but it seems with these testing libraries that the whole "convention over configuration" thing is turned on its head. There's a hell of a lot of configuration.
I simply want to run all of my tests. How can I do that?
What does your Rakefile look like? You may need to add the following:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
Also, check that your spec's filenames all end in _spec.rb.
Just this will do:
bundle exec rspec spec
This must be fired from root directory of application. Here spec is the directory.

RSpec Can't find Ruby Files/Classes

I'm using rspec 2.10 with Rails 3.2.1 and have problems with classes not being found.
I have put a class called DbTasks under a dir app/util. Under spec, i've created file util/db_tasks_spec.rb which runs afer i included the line:
require File.join(File.dirname(__FILE__), '../../app/util/db_tasks')
However, rspec now isn't able to find my models. I don't want to have to include these manually. Is there a way to config rspecs paths so everything gets found?

Resources