3

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:

  1. It's hacky
  2. 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!

nichobot
  • 61
  • 6

1 Answers1

2

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

nichobot
  • 61
  • 6