How to TDD the following snippet in Ruby using Eclipse on Windows?
class PassIntegers
def addition(i,j)
k = i+j
end
end
Install test-unit by executing gem install test-unit
Run the following snippet as 2 Ruby Test
require 'test/unit'
require 'PassIntegers'
class TestPassIntegers < Test::Unit::TestCase
def test_pass_integers
passIntegers = PassIntegers.new
expected = passIntegers.addition 3,2
assert_equal expected, 5
end
end
Possible issue
If the following error occurs at the Eclipse Console:
C:/dev/tools/eclipse/configuration/org.eclipse.osgi/bundles/965/1/.cp/testing/dltk-testunit-runner.rb:252:in `block in <top (required)>': uninitialized constant Test::Unit::UI::SILENT (NameError)
Open C:\dev\tools\eclipse\configuration\org.eclipse.osgi\bundles\965\1\.cp\testing\dltk-testunit-runner.rb and solve this by commenting
#autoRunner.output_level = Test::Unit::UI::SILENT
and adding
autoRunner.runner_options[:output_level] = Test::Unit::UI::Console::OutputLevel::SILENT
and run the test again.
Solution to solve the uninitialized constant Test::Unit::UI::SILENT (NameError) issue found at http://blog.christopherkaiser.de/?p=319
Related
I'm confused with the difference between Object#singleton_method and Object#singleton_methods.
I thought that the result in Object#singleton_methods is the true set of !!Object#singleton_method(:name), but it seems to be different.
Here is the example script:
require "active_support/deprecation"
# [:debug=, :debug]
ActiveSupport::Deprecation.singleton_methods(false).grep(/debug/)
# [:debug=, :debug]
ActiveSupport::Deprecation.singleton_methods.grep(/debug/)
begin
ActiveSupport::Deprecation.singleton_method(:debug) # exception
rescue => e
puts e.backtrace
raise
end
Gemfile is
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "rails"
gem 'activesupport', '5.1.6'
The result is this:
% bundle install
Fetching gem metadata from https://rubygems.org/..............
Resolving dependencies...
Using concurrent-ruby 1.0.5
Using i18n 1.0.0
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.1.6
Using bundler 1.16.1
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
% bundle exec ruby test.rb
test.rb:8:in `singleton_method'
test.rb:8:in `<main>'
Traceback (most recent call last):
1: from test.rb:8:in `<main>'
test.rb:8:in `singleton_method': undefined singleton method `debug' for `ActiveSupport::Deprecation' (NameError)
I expected that ActiveSupport::Deprecation.singleton_method(:debug) would return #<Method: ActiveSupport::Deprecation.debug>, but raise exception.
I don't understand why. Of course, I know the basic usage in singleton_method and singleton_methods:
# everything is OK!
class Sample; end
class << Sample
def test_method; end
end
# These two expressions behave as I expect.
## [:test_method]
Sample.singleton_methods(false).grep(/test_method/)
## #<Method: Sample.test_method>
Sample.singleton_method(:test_method)
Thanks.
Update: It looks like this is a bug in Ruby. Vasiliy Ermolovich has created an issue along with a patch to address it. The fix has already been merged so this will be resolved in an upcoming update.
This isn't a full answer to your question I'm afraid, but after a bit of digging I've been able to make a minimal example that demonstrates the same thing without the dependency on Active Support. I thought this might still be useful to you for your investigations, or might help someone else who can come along with a complete explanation.
It appears that the unexpected behaviour of singleton_method comes from use of Module.prepend which ActiveSupport::Deprecation uses here.
The same error can be seen with this small example:
module Empty; end
class MyClass
singleton_class.prepend(Empty)
def self.my_method
puts "my method called"
end
end
p MyClass.singleton_methods(:false)
m = MyClass.singleton_method(:my_method)
m.call
Running this gives:
❯ ruby example.rb
[:my_method]
Traceback (most recent call last):
1: from example.rb:11:in `<main>'
example.rb:11:in `singleton_method': undefined singleton method `my_method' for
`MyClass' (NameError)
With the call to prepend removed this runs as you would expect:
❯ ruby example.rb
[:my_method]
my method called
I have a ruby extension written in C++, P4, and it seems to generally work:
I can run irb -Ilib and then require 'P4', and use it
I can execute tests via rake by accessing the shell script in the bin folder of the rake gem, e.g., ${GEM_HOME}/gems/rake-10.3.2/bin/rake test
however, when I access rake via the RubyGems wrapper in my path, e.g., rake test, I get this TypeError
/Users/tjuricek/dev/p4ruby/lib/P4.rb:38:in `require': P4 is not a class (TypeError)
from /Users/tjuricek/dev/p4ruby/lib/P4.rb:38:in `<top (required)>'
from /Users/tjuricek/dev/p4ruby/test/testlib.rb:31:in `require'
from /Users/tjuricek/dev/p4ruby/test/testlib.rb:31:in `<top (required)>'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:15:in `require'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:15:in `block in <main>'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:4:in `select'
from /Users/tjuricek/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Then it pops out the ruby command that "failed". If I copy and paste that command and run it, it works.
What I've noticed is that RubyGems creates a fairly simple wrapper script:
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end
gem 'rake', version
load Gem.bin_path('rake', 'rake', version)
I'm guessing that the last line, load Gem.bin_path... has triggered some kind of misconfiguration of my part in creating my extension, but I have no idea what that would be.
Does anyone have ideas on what might cause require to fail only when run under the RubyGems wrapper?
OK, the way to debug this is to go into a line where you load the file:
require 'P4.so'
And see if it's defined:
puts "P4 #{P4.class}"
require 'P4.so'
In this case, when running under rake directly, it loaded the .gemspec which pulled in a version definition that created (incorrectly, in my case) a P4 module:
module P4
version = VERSION = '3000.0.0.pre0'
end
So, for me the fix included:
Changing module P4 to class P4
Requiring the version definition before my require 'P4.so' statement, typically: require_relative 'P4/version'
Not defining the class in my C++ extension code, but loading it and extending the one defined in my version file:
// Ensure the class has been defined by the version specification
cP4 = rb_path2class("P4");
NoMethodError: undefined method `assert_true'
Am getting above error while running tests using test-unit in ruby. test-unit gem and rake versions are below,
test-unit (2.5.5)
rake (10.1.0)
Sample test file:-
require 'test/unit'
class Sample < Test::Unit::TestCase
def setup
# code block
end
def test_sample
assert_true("test"=="test")
end
def teardown
# code block
end
end
How to solve this ?
I solved the problem by using following way. No need to change assert statements.
require 'rubygems'
gem 'test-unit'
require 'test/unit'
class Sample < Test::Unit::TestCase
def setup
# code block
end
def test_sample
assert_true("test"=="test")
end
def teardown
# code block
end
end
assert_true does not appear to be in the list of available assertions for test-unit. Try using assert. Reference: http://ruby-doc.org/stdlib-2.0.0/libdoc/test/unit/rdoc/Test/Unit/Assertions.html
Since 1.9.2, test/unit is a wrapper around minitest, implemented directly in the ruby source code. The assert_true method does not exist in the new implementation, just use assert instead, as Simon Brahan already suggested. So the gem source you were looking at is no longer in use. The now relevant documentation is here.
I'm using:
Rails 3.0.7 and
Rspec 2.5.0
via rvm
When I run this spec (using autotest or bundle exec autotest or bundle exec rspec spec/) below:
require 'spec_helper'
require 'yaml'
def twitter_feed(id=1)
ruby_object = YAML.load_file(::Rails.root.to_s + "/spec/fixtures/feeds/twitter_response_#{id}.yml")
end
I get this:
Failure/Error: ruby_object = YAML.load_file(::Rails.root.to_s + "/spec/fixtures/feeds/twitter_response_#{id}.yml")
TypeError:
invalid subclass
# ./spec/models/tweet_spec.rb:6:in `twitter_feed'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `transfer'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `node_import'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `load'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:133:in `load'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:144:in `load_file'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:143:in `open'
# /Users/natebean/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/yaml.rb:143:in `load_file'
# ./spec/models/tweet_spec.rb:5:in `twitter_feed'
# ./spec/models/tweet_spec.rb:58
This "was" working. I can't find any other information on this error on the internet. I've moved from rails 3.0.3 to 3.0.7, but don't remember it not working after the upgrade.
Any suggestions? Thanks.
The yaml file I was pulling was looking for Hashie::Mash to map the data to. Up to know I didn't need to require 'hashie', but that has "fixed this problem".
I added to this to my spec and it is now working.
require 'hashie'
Run bundle exec rspec spec --backtrace to get a full backtrace so you can see exactly where that error is coming from.
I'm trying to write a Ruby plasmoid for KDE. I need to use barely one rubygem. Whenever I write require 'dbus', it throw me and an error:
code/main.rb:6:in 'require': no such file to load -- dbus (LoadError)
code/main.rb:6:in '<module:TestApp>'
code/main.rb:5:in '<top (required)>'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:177:in 'load'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:177:in 'init'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:201:in 'constraintsEvent': undefined method 'constraintsEvent' for nil:NilClass (NoMethodError)
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:201:in 'constraintsEvent': undefined method 'constraintsEvent' for nil:NilClass (NoMethodError)
Actually, normal "ruby main.rb" works well (regarding on "require" part), but testing plasmoid with "plasmoidviewer" fails. Note, that regular gems from standart Ruby installation works well, i.e. require 'Qt4' or require 'yaml' loads perfectly. I'm using Ruby 1.9.2p180 under Linux.
09:40 PM - UPDATE: Richard Dale, one of the QtRuby developers, just fixed this issue a few minutes ago. Next release of KDE will have patched version of QtRuby.
require 'find'
require 'findUtils'
Find.find(PATH_WHERE_GEM_IS_INSTALLED) do |path|
if FileTest.directory?(path)
$: << File.expand_path(path)
if File.basename(path)[0] == ?. and File.basename(path) != '.'
Find.prune
else
next
end
else
end
end
and after that you can do
require 'dbus'
Have you tried this:
require 'rubygems'
?