How can 'parse' be undefined for Time class in test environment, but not development or production? - ruby-on-rails

I'm encountering a strange issue while running a test (Steak + Capybara) that requests a certain page where I get the error:
undefined method parse for Time:Class
The method call occurs in a Sunspot file called type.rb:
def value_to_utc_time(value)
if value.respond_to?(:utc)
value.utc
elsif value.respond_to?(:new_offset)
value.new_offset
else
begin
Time.parse(value.to_s).utc
rescue ArgumentError
DateTime.parse(value.to_s).new_offset
end
end
end
When I run my server in RAILS_ENV=test, I get the same error. When I run the server in development, everything is fine and dandy. This would seem to indicate that it's not just my test framework, but the whole test environment that's promoting this error.
The only info I could find online so far about Time.parse being undefined has been quite unhelpful, with most people saying that adding require 'time' will solve the problem.
My problems with this are twofold:
It's hacky
I've tried it in various places (in config/environments/test.rb, in the test file itself, etc.), and it just doesn't work.
If anyone has any ideas what is going on, I welcome them!

I figured it out! Someone had created another file called time.rb in the Rails app's test directory, so method calls on the Time class in the test environment were invoking that file rather than the actual Time class. I got the inspiration to check for this possibility from this Github issue thread

Related

Rspec with elasticsearch (searchkick) fails on CI server (Codeship)

I'm having problems getting my specs to run cleanly on our CI server (Codeship).
The specs run fine locally but it seems that on the CI server elasticsearch is having issues keeping in sync.
I've researched this issue and have found potential solutions, but none of them work.
First solution I had, was creating a helper method for the test suite for whenever I needed to reindex.
def refresh_elasticsearch(model)
model.reindex
model.searchkick_index.refresh
end
Without this I was having local specs fail because documents were not being found. Once I started calling the helper method, everything started working.
I have 2 controllers that have elasticsearch functionality, and only one of them runs OK on Codeship, which is strange. These controllers are very similar to one another so I'm really confused why one controller spec passes ALL the time and the other one doesn't.
No exceptions are thrown, just some basic expectations that fail because I'm expecting a document and it finds none.
I've tried all these solutions as well:
https://github.com/ankane/searchkick/pull/95
All these solutions work locally BTW. But they all fail for the same reason on the CI server.
I'm running out of things to try at this point. Any ideas??
Any help is appreciated. Thanks!!

No ".includes?" ruby method when running RSpec Test

I've recently got into RSpec and i'm writing some tests for my code, one of things my code has is a section where it starts by going if category.name.includes?(matcher) and then it does something, running this code in production is fine however whenever i'm testing that piece of code with RSpec I get the following error:
undefined method 'includes?' for "category21":String
To clear some things up, my factories do clearly define the categories name, i'm mainly wondering if I need to include some form of library to get it to play nicely?
Thanks, Grant
You should use include?, not includes?.

Rails: Reloading code in development environment gives a "ApplicationHelper has been removed from the module tree but is still active!" error

I upgraded to Rails 4 and now when some changes are made to the code, or when I do a reload! in rails console, I will get an ArgumentError: A copy of ApplicationHelper has been removed from the module tree but is still active! error.
I'm not the only one with this problem but still cant find existing solutions that work yet.
I've never encountered that problem before. I'm using Rails4 as well.
I think you have to call your controller(which is shown in the ArgumentError) explicitly in your dependent controllers.
Can you try adding this line on top inside the class?
require "_controller"
Then try rails server -e production
See if this helps. Sorry I wish I could give you more detailed solution.
If all else fails, rollback might be an option to consider.

Rolify dynamic calls not run or recognized by heroku

I am getting an error for using a function provided by a special configuration on Heroku even though it works fine on my machine.
Specifically, I am using the Rolify method is_admin? on user, which is made possible by a configuration setting in an initializer file.
So far with Heroku I have migrated the database again, restarted the app and refreshed the page multiple times for each. How can I get Heroku to run the configuration file like my local machine does every time I reset the local server?
Error:
ActionView::Template::Error (undefined method `is_admin?' for #<User:0x0000000524c8e0>):
2012-07-22T09:00:53+00:00 app[web.1]: 1: <% if current_user.is_admin? %>
Code:
# config/initializers/rolify.rb
Rolify.configure do |config|
config.use_dynamic_shortcuts
end
Well, I tried one more thing and it worked, but here it is just in case.
I ran $ heroku run rake db:reset and it worked again. Not sure why, but oh well.
Edit:
The issue is that if you call Rolify custom methods when the role does not exist yet in the database, it results in an error. To get around this, either seed your database with all of the roles you'll need, or better, do not use dynamic methods.
For example, instead of:
#user.is_admin? use #user.has_role?(:admin) and it will not break.

Rails moving from 2.2.2 to 2.3.8 - rake not loading the environment same as the console or my app

I have an app that has been around a while that I'm migrating for
Rails 2.3.8. There have been a lot of interesting issues, but I'm on
the home stretch, but have now run into some very odd errors.
The gist is when I run a task with rake it fails, but when I run the
same code form the console it works fine. The 'fix' I devised was to
move my include ModuleNameA, include ModuleNameB, etc to the bottom of
the AR file.
I don't like this 'fix' as I don't understand it. Before I moved them
the AR object (we will call it Bob) kept throwing undefined method
errors when it was called through the rake invocation. It was invoked
in the rake task, but in another AR object.
So the rake task was running something like Worker.work, and it would
fail as described above. When I run Worker.work from the console it
would pass. Once I moved the includes to the bottom of the file both
would work.
Anyone ever seen anything like this? I feel like something
fundamental isn't correct. Like I have broken something basic to the
functionality and my 'fix' is some kind of weird patch.
thanks.
Erik
PS:
There is a module that I'm including in my AR object. It adds class and instance methods by doing:
def self.included(base)
base.extend(ClassMethods)
end
All of the methods in this module are available in the console, but not through rake tasks.
Update:
I noticed that if i take the :work => :environment part out of the
rake task and instead in the task do block require the environment
file by hand (e.g. require(File.join(RAILS_ROOT, 'config',
'environment'))) it works fine. All of a sudden all of my class
methods are available. this is very disturbing.
So I figured it out....
We had a directory under app/models called deals that was not included in the load path. That is where my module was that was giving me all the issues. Why it was being loaded OK from the console and not with rake...I don't know.
Once I added this to the config.load_path everything has started working fine. I never noticed this because in 2.2.2 everything worked fine, the app in production or dev mode, the console and the rake tasks in dev, test, and production mode.
I figured it was something just basic i was doing wrong as the error was obtuse and my 'fixes' looked like really bad ideas.

Resources