Tests suddenly erroring out. Anyone seen this error? - ruby-on-rails

Ruby 1.8.6, Rails 2.2.2, OS X Tiger
My Test::Unit tests started returning the error below. The relevant line seems to be:
`load_missing_constant':
Expected /Users/ethan/project/mtc/webcalendars/app/models/calendar.rb
to define Calendar (LoadError)
The file mentioned, calendar.rb looks fine. I can't find any errors in it. I tried removing the unit and functional test files for the Calendar model, but that had no effect on the error.
In the browser the application seems to be functioning normally.
Any insights? Can anyone suggest a way to isolate the problem?
Longer excerpt:
$ rake test
(in /Users/ethan/project/mtc/webcalendars)
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb"
"test/unit/calendar_event_test.rb" "test/unit/calendar_test.rb" "test/unit/committee_test.rb"
"test/unit/event_test.rb" "test/unit/general_app_mailer_test.rb" "test/unit/location_test.rb"
"test/unit/persistent_login_test.rb" "test/unit/role_test.rb" "test/unit/user_role_test.rb"
"test/unit/user_test.rb"
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:428:in
`load_missing_constant': Expected /Users/ethan/project/mtc/webcalendars/app/models/calendar.rb to define Calendar (LoadError)
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:77:in `const_missing'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:89:in `const_missing'
[ ... ]
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib
/rake/rake_test_loader.rb"
Errors running test:units and test:functionals!

Ethan,
The only time I've seen this is when I've defined multiple classes in one file. I don't use test:unit too much myself but is it possible that you've defined another class before defining Calendar?
Hope that helps.

Related

Rails - ckeditor orm failed to install correctly

So i made a bit of a mistake when trying to add ckeditor to my Rails project last night.
I was following a tutorial and it came to a point in the installation of this gem where it told me to input the following command:
rails generate ckeditor:install --orm=active_record --backend=carrierwave
except i misspelled 'record'
I've looked everywhere i can think and i cant seem to fix this, rails d ... doesnt work gem uninstall doesnt fix it either, and i can't see any related files in my db folder, or anywhere else that i know to look at.
heres the first bit of the error i keep seeing:
/home/naazarik/.rvm/gems/ruby-2.3.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require': cannot load such file -- ckeditor/orm/active_recort (LoadError)
from /home/naazarik/.rvm/gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
notice the misspelled active_recort?
anyways does anyone have any idea how to either fix this, or remove this from my project?
Thanks!
CKEditor installer should generate config/initializers/ckeditor.rb file that in your case will have following line: require 'ckeditor/orm/active_recort', which you can update to say active_record instead
However, ActiveRecord integration requires a few more files to be generated (and these files were not generated due to a misspelling). Because of that I believe the best way to fix the problem is just to remove the initializer file entirely and run the rails generate ckeditor:install command again.

Creating models from lib directory in Rails 4

I have a file lib/stock_reader.rb in which I'm trying to create a model like so:
module StockReader
def self.create_company_reports(company_data)
CompanyReport.create(name: company[:name])
end
end
In another file, lib/curate.rb, I call this method:
require_relative 'stock_reader'
StockReader.create_company_reports(company_data)
But I receive the error:
/Users/me/code/applications/curator/lib/stock_reader.rb:38:in `block in create_company_reports': uninitialized constant StockReader::CompanyReport (NameError)
from /Users/me/code/applications/curator/lib/stock_reader.rb:37:in `each'
from /Users/me/code/applications/curator/lib/stock_reader.rb:37:in `create_company_reports'
from lib/curate.rb:12:in `<main>'
It seems that my lib directory is failing to recognize my model's existence in app/models/company_report.rb:
class CompanyReport < ActiveRecord::Base
end
I'm guessing this may be because the lib/ directory is being loaded before app/models, but I'm not sure.
I've looked at Accessing models from within the lib directory in a Rails 3 project but I can't see where my lib/ directory is being required in any rakefiles.
It happens when I test lib/curate.rb by running $ ruby lib/curate.rb
Well, that would explain it.
Ruby doesn't know anything about CompanyReport. Rails knows where to find your models because it has a ton of code that handles autoloading classes, but Ruby isn't Rails. If you want code to use Rails' features, you need to run the code in the "Rails environment."
There are a few ways to do this. If you want to run an arbitrary script (like lib/curate.rb) in the Rails environment, you can use the rails runner command:
$ bin/rails runner lib/curate.rb
The Rails console is also very useful for testing:
$ bin/rails console
Loading development environment (Rails 4.1.6)
irb(main):001:0> require Rails.root + "lib/curate"
It's pretty rare to use the ruby command in a Rails project, because usually you want to use Rails' features. You'll probably use the above commands a lot more often.
The problem is that CompanyReport class does not exist in the StockReader module where it's looking for it. To use the top level CompanyReport model preface the class name with ::
::CompanyReport.create(name: company[:name])

Error when trying to run rspec: `require': cannot load such file -- rails_helper (LoadError)

I am trying to run rspec for Ruby on Rails. I am running Rails 4.1.1. I have installed the gem, have established a spec folder with some tests. I have created a directory through $ rails g rspec:install
I tried to create a testing database through $ rake db:test:prepare but it throws this error message:
WARNING: db:test:prepare is deprecated. The Rails test helper now maintains your test
schema automatically, see the release notes for details.
So I ended up looking at this stack overflow post, and of the two options, the one that worked was:
rake db:schema:load RAILS_ENV=test
So, now I need to run rspec.
When I run $ rspec spec from the command line I get this error:
/Users/myname/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/
kernel_require.rb:55:in `require': cannot load such file -- rails_helper (LoadError)
How do I resolve this so that I can start running tests?
There is some problem with rspec V3. But in your case you are using V2.
change
require 'rails_helper'
to
require 'spec_helper'
Other description find here https://teamtreehouse.com/forum/problem-with-rspec
For V3 :
If someone using rspec V3 then similar error occurs when generator not run. So before trying anything run generator.
rails generate rspec:install
If you are getting a huge list of warning on your console. Then you need to remove --warnings from .rspec file.
I actually just had this error on rails 4 with rspec 3, but in my case I forgot to run the generator:
rails generate rspec:install
Also I had to remove warnings from .rspec, as stated by one of rpsec developers
For me, the problem was due to a different "rspec-support" version loaded and activated. I solved by prepending a "bundle exec":
bundle exec rspec
Always remember to run the
rspec or the bundle exec rspec from the root of your project.
Something like:
bundle exec rspec spec/features/file_test_spec.rb
For newer versions (3.5), if you need to run the generator, rails generate rspec:install doesn't work for me. I used:
rspec --init
Creates .rspec and spec/spec_helper.rb.
EDIT:
Just in case, I found out that I installed rspec gem for ruby, not the one for Rails, so rspec:install wasn't available. Should use https://github.com/rspec/rspec-rails.
Sometimes you need to run rspec command from Project's parent directory
Please install the following gem:
gem install 'rspec-rails'
And then run:
rails generate rspec:install
This will generate the spec folder and rails_helper.rb and spec_helper.rb. Your error should be taken care once this is done.
None of these suggestions worked for me on Rails 4.2.0 with rspec-rails 3.2.3.
Instead, I placed the following two lines at the top of my spec file:
require 'rails_helper'
require 'spec_helper'
Now I can execute rspec spec/features/my_spec.rb.
I had the same error but there was no reference to rails_helper anywhere and we weren't even using rails.
However, I eventually found that my ~/.rspec was requiring rails_helper (presumably from creating rails app in the past) which was causing the issue ...
$ cat ~/.rspec
--color
--format documentation
--require spec_helper
--require rails_helper
The fix is as simple as removing that line. Hope this helps someone else grappling with the same issue!
One possibility that worked for me is to make sure you're requiring spec_helper. RubyMine can default require rspec and this can sometimes lead to this error.
You may also have your file named incorrectly. Make sure it ends in _spec.rb. I had misspelled my file to end in _soec.rb and was scratching my head for a while...
I got the error you described when running a controller test. However, when I ran a model test, I got the true error. it ended up saying that the error was occuring because my database wasn't migrated. I fixed the error by running this command...
rake db:test:prepare
For someone. If you got that error when you trynna test a specific file. you might can like this
$ bundle exec rspec ./spec/features/user_login_spec.rb
$ bundle exec rspec ./spec/models/user_login_spec.rb

Installing Mocha 0.9.7 in a Rails 2.3.3 project

I installed the Mocha 0.9.7 Rails plug-in using:
$ script/plugin install git://github.com/floehopper/mocha.git
(Just followed instruction in http://mocha.rubyforge.org/)
Then, I have the following set-up defined in my functional test
def setup
#controller.expects(:logged_in?).returns(true)
#controller.expects(:admin_user?).returns(true)
end
Running the test generates the ff. error:
NameError: uninitialized constant Mocha::Mockery::ImpersonatingName
/test/functional/xxxx_controller_test.rb:x:in `setup'
Before that, I see the ff. error at the top of the test log:
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/test_case.rb:12: warning: already initialized constant Mocha
This has led me to believe that I have an old version of Mocha somewhere in Ruby's or Rails' path. The problem is I can't find it.
Is my guess correct? If so, where is this old version of Mocha? Alternatively, how can I found out where it is?
According to http://selfamusementpark.com/blog/2009/07/30/rails233mochaconfusion/, my guess is not correct. The problem really is that Mocha is being loaded before the testing framework which is not what the former expects. The solution is to edit RAILS_ROOT/vendor/plugin/mocha/init.rb to comment out the ff. line:
require 'mocha'
Then, Mocha will have to be explicitly required in the test files or helpers to ensure that the testing framework will have been loaded beforehand.

rake spec not using the rails environment

I'm attempting to use rspec in a rails project I've just upgraded to rails 2.3.2. I've installed rspec 1.2.6 and rspec-rails 1.2.6 as plugins in the app.
My problem is the specs don't have access to my app classes or any of the rails standard libraries.
First I had to specify the model class I want to test by using the full path from RAILS_ROOT but now as it loads the class I get the following
/app/models/person.rb:1: uninitialized constant ActiveRecord (NameError)
from ./spec/models/person_spec.rb:1:in `require'
from ./spec/models/person_spec.rb:1
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:15:in `load'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:15:in `load_files'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in `each'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in `load_files'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/options.rb:99:in `run_examples'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/command_line.rb:9:in `run'
from /Users/law/Projects/roster/vendor/plugins/rspec/bin/spec:4
rake aborted!
I am launching rspec by calling rake spec from the root of the application.
Any ideas on what might be missing in this situation?
you need indeed include the spec_helper.rb in every spec file you write....
You can run individual specs that way:
$ spec specs/models/person_spec.rb
instead of always running the whole spec suite
I havn't used spec, so this may not solve your problem, but if you're writing your own rake task and need your rails environment, you have to ask for it.
task(:task_name => :environment) do
# Task Implementation Here
end

Resources