I am trying to create a custom rspec formatter using Watir and got this error while executing the class. I have never used Watir before. Do I need some special gems for it?
Error: uninitialized constant Watir::RSpec::Core (NameError)
My code:
require 'rspec/core/formatters/html_formatter'
module Watir
class RSpec
class CustomFormatter < RSpec::Core::Formatters::HtmlFormatter
end
end
end
The exception is occurring because of the class that the CustomFormatter is trying to inherit from. Due to the location, it is look for the RSpec::Core::Formatters::HtmlFormatter class within the scope of the Watir::RSpec class.
Usually custom formatters are done as:
require 'rspec/core/formatters/html_formatter'
class CustomFormatter < RSpec::Core::Formatters::HtmlFormatter
end
You do not need to put it in a Watir::RSpec space.
Related
I have these 2 files in a large system, both are located in PackA
people.rb
module People
class HobbyValueObject
end
end
job.rb
module People
class Job
end
class CityValueObject
end
end
I am trying to use CityValueObject in a different module like this,
Module is in PackB
work.rb
module Profile
class Work
....
def calculateTaxes
...
a = People::CityValueObject....
end
end
end
But it's giving me an error saying,
NameError: uninitialized constant People::CityValueObject
Did you mean? People::HobbyValueObject
Why is not being able to fine CityValueObject but can find HobbyValueObject just fine?
How do I make it find the object that I am intending to use?
I am not explicitly declaring any requires or includes
I was able to resolve this by adding require at the top while using full path file name.
require './packs/people/app/public/people/job'
I've worked with a couple of Ruby gems and also Rails. One thing I've never fully understood is why Rails required explicit class constant references for code defined in the /lib folder. In a ruby gem, I could create something like this:
lib/my_gem/custom_error.rb
module MyGem
class CustomError < StandardError
end
end
lib/my_gem/some_class.rb
module MyGem
class SomeClass
def initialize
raise CustomError
end
end
end
Whether it's an error, another class or whatever, as long as the calling class is in the same namespace as the referenced class, ruby would initialize the correct class CustomError in the case above. Moving to Rails, this is a different story and this code would result in an uninitialized constant error. In Rails I would have to raise MyGem::CustomError instead. Why is this the case? I assume it has something to do with autoloading. Is there a way around this or is this standard?
The module that I am trying to include is located here:
test/unit/helpers/test_helpers.rb
Looks like:
module TestHelpers
end
I am trying to include it in:
test/unit/app/models/abc.rb
class Abc < ActiveSupport::TestCase
include TestHelpers
end
gives the following error:
Error executing test/unit/app/models/abc.rb uninitialized constant
Abc::TestHelpers
Any ideas why this might be happening?
To include a module into your class, you need to require that file.
require 'test_helpers'
Add this line at the top of your model class.
In Rails you can create a model under app/foo/bar.rb, with bar.rb containing:
class Foo::Bar
def some_method
puts "I work fine"
end
end
If you try to do this in a pure ruby app you'd get a NameError: uninitialized constant Foo unless you've already initialized a module Foo.
What is Rails doing that allows it to create classes without first initializing their containing module? Is it possible to import this behavior through something like activesupport, or are we left to implement on our own?
Rails modifies the Class class to include a const_missing method which gets called when an undefined class is used. It then loads things to try and load the requested class.
The implementation of this in ActiveSupport is in lib/active_support/dependencies.rb.
actually model class created is extend to < ActiveRecord::Base
I am trying to access the constant VALID_FIND_OPTIONS defined in ActiveRecord::Base (active_record/base.rb Line 2402 Rails 2.3.5).
ActiveRecord::Base::VALID_FIND_OPTIONS
I get the NameError exception.
NameError: uninitialized constant ActiveRecord::Base::VALID_FIND_OPTIONS
I have accessed the class constants in other libraries using the similar syntax before. I am not sure where I am going wrong.
The constant VALID_FIND_OPTIONS was defined inside the anonymous class of ActiveRecord::Base, hence it was not accessible as ActiveRecord::Base::VALID_FIND_OPTIONS
module ActiveRecord
class Base
class << self
# the constant belongs to the scope of the anonymous class
VALID_FIND_OPTIONS = [..]
end
end
end
The constant can be accessed using following syntax:
ActiveRecord::Base.singleton_class::VALID_FIND_OPTIONS
Where is the code that tries to get ActiveRecord::Base::VALID_FIND_OPTIONS?
If you are defining a class before ActiveRecord is loaded, then the constant will not be available.
You can force ActiveRecord to be loaded by requiring it. In some cases, you will have to require rubygems before requiring active_record.
Try requiring them both:
require 'rubygems'
require 'active_record'
# you should now be able to access ActiveRecord::Base::VALID_FIND_OPTIONS