Ruby test unit: uninitialized constant Test::Unit::UnitCase - ruby

I am trying to run this test, but I am running int a problem. Every time that I attempt to run the test, I am getting the following error:
uninitialized constant Test::Unit::UnitCase
I am running it on Windows 7, ruby 186.
Path points to C:\ruby186\bin
Ruby Bin C:\ruby186\bin
This is not a rails app. This is just ruby.
This is the code:
require 'rubygems'
require 'test/unit'
require 'shoulda'
require './AngryBadger'
class AngryBadger < Test::Unit::UnitCase
def setup
test_uri = ""
#ab = AngryBadger.new()
end
should "have no fear" do
assert(true)
end
end
I suspect that this can be fixed through configuration, but I don't know what I could do.

You should be subclassing from Test::Unit::TestCase instead:
class AngryBadger < Test::Unit::TestCase

Related

How to write a Ruby (Minitest) unit test that fails if I don't have the correct require statements?

I have this test:
require 'minitest/autorun'
require 'minitest/color'
require_relative '../lib/util/input_file'
class TestInputFile < Minitest::Test
def setup
#input_path = Pathname.new("/path/to/inputs")
end
def test_default_input_file
input_file = Util::InputFile.new(1)
expected_path = #input_path.join('input01.txt')
assert_equal(expected_path, input_file.abspath)
end
# more tests follow
end
for this code:
module Util
class InputFile
def initialize(num)
#num = num
#input_dir = Pathname.new("/path/to/inputs")
end
def abspath
basename = 'input'
return #input_dir.join(format('%s%02d.txt', basename, #num))
end
end
end
When I run this with rake test, everything passes as expected; however, when I call it from my actual main script, it chokes with uninitialized constant Util::InputFile::Pathname (NameError). When I add require 'pathname' at the top of lib/util/input_file.rb, everything is fine.
Why does the unit test not fail in the same way, and how can I refactor it such that it will fail unless I have the correct require statement in the production code?
EDIT: Rakefile is as follows:
require 'minitest/test_task'
Minitest::TestTask.create do |t|
t.test_globs = ['test/**/test*.rb']
end
To run tests separately use rake test:isolated instead of rake test.
https://github.com/minitest/minitest#rake-tasks-
It seems like one of your other tests gets Pathname loaded.
How does your Rakefile look and doesn't it get pathname required? It can explain why your test (started with rake test) goes well.
And why don't you want to require pathname at the top of lib/util/input_file.rb?

uninitialized constant BikeShare (NameError)

I'm trying to implement some simple testing in rspec for a gem I'm writing. When I comment out describe BikeShare do down to end and run the file, the file loads in and runs successfully. I'm sure it's something tiny I'm missing.
My test file is really simple and looks like this:
require 'spec_helper'
describe BikeShare do
it "should run" do
# response = BikeShare.new
# response.should_be present
end
end
When run, I get the error uninitialized constant BikeShare (NameError) at line 3.
My bikeshare.rb file looks like this, fairly simple:
class BikeShare
def initialize
response = JSON.parse(open("http://bayareabikeshare.com/stations/json").read)
#response = response["stationBeanList"]
end
def get_last_station
#response.last["id"]
end
end
My Rakefile looks like this:
require 'rubygems'
require 'bundler'
Bundler.setup
Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |spec|
# spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/*_spec.rb'
end
task :default => :spec
Your tests arent aware of BikeShare.
You need to require the file that defines your BikeShare class. I dont use rspec but I think that you normally set up your testing environment in spec_helper.rb.

Use test/unit with anonymous TestCase

This question belongs zu test-unit version 2.5.3
Problem solved with test-unit version 2.5.4
I have a test with many anonymous TestCases. It worked with test-unit 2.5.0, but the actual version 2.5.3 produces an error.
When I run this test:
gem 'test-unit', ">=2.5.2"
require 'test/unit'
Class.new( Test::Unit::TestCase ) do
def test_add
assert_equal( 2, 1+1)
end
end
no test is executed and I get the error undefined method sub' for nil:NilClass (NoMethodError) in testrunner.rb:361 (I use the actual test-unit-gem 2.5.3).
With a name for the TestCase, the problem disappears:
gem 'test-unit'
require 'test/unit'
X = Class.new( Test::Unit::TestCase ) do
def test_add
assert_equal( 2, 1+1)
end
end
In my real problem, I generate many TestCases. So I have a situation like:
gem 'test-unit'
require 'test/unit'
2.times {
X = Class.new( Test::Unit::TestCase ) do
def test_add
assert_equal( 2, 1+1)
end
end
}
If I execute this I get a warning already initialized constant X and the error:
comparison of Array with Array failed (ArgumentError) (in collector.rb:48:in sort_by').
My question(s):
How can I avoid the error?
Or: How can I create TestCases with dynamic assigned constants?
It seems this is down to a change in the latest version of the test-unit gem, which now requires a readable name for a class.
Something like this will work
gem 'test-unit', ">=2.5.2"
require 'test/unit'
Class.new( Test::Unit::TestCase ) do
def test_add
assert_equal( 2, 1+1)
end
def self.to_s
"GeneratedClass"
end
def self.name
to_s
end
end

NoMethodError validate_presence_of using shoulda

Ok, I'm baffled. I'm trying to use shoulda with Test::Unit under Rails 3.1, having previously done so successfully with Rails 2.3.11.
I have the following in my Gemfile:
group :test do
gem 'shoulda'
end
(and I've run bundle install - bundle show shoulda shows c:/Ruby192/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3)
I have the following test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'shoulda'
class ActiveSupport::TestCase
end
and the following user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should validate_presence_of :email
should validate_format_of(:email).with("user+throwaway#subdom.example.com").with_message(/valid email address/)
should validate_presence_of(:encrypted_password)
should validate_confirmation_of :password
end
But when I do ruby -Itest test\unit\user_test.rb, I get the following error:
test/unit/user_test.rb:4:in `<class:UserTest>': undefined method `validate_presence_of' for UserTest:Class (NoMethodError)
What have I failed to set up properly?
Solved it. You need:
require 'shoulda/rails'
in test_helper.rb (not `require 'shoulda'); and the test case needs to be:
class Usertest < ActiveRecord::TestCase
(not < ActiveSupport::TestCase).
I'd tried both of those individually, but not together...

Ruby Watir can't find the assert method outside of the running class?

I have a class that I want to use in many test cases:
require 'rubygems'
require 'test/unit'
require 'watir'
class Tests < Test::Unit::TestCase
def self.Run(browser)
# make sure Summary of Changes exists
assert( browser.table(:class, "summary_table_class").exists? )
# make sure Snapshot of Change Areas exists
assert( browser.image(:xpath, "//div[#id='report_chart_div']/img").exists? )
# make sure Integrated Changes table exists
assert( browser.table(:id, 'change_table_html').exists? )
end
end
However, when run in one of my test cases:
require 'rubygems'
require 'test/unit'
require 'watir'
require 'configuration'
require 'Tests'
class TwoSCMCrossBranch < Test::Unit::TestCase
def test_two_scm_cross_branch
test_site = Constants.whatsInUrl
puts " Step 1: go to the test site: " + test_site
ie = Watir::IE.start(test_site)
Tests.Run(ie)
end
end
I get the error:
NoMethodError: undefined method `assert' for Tests:Class
C:/p4/dev/webToolKit/test/webapps/WhatsIn/ruby-tests/Tests.rb:8:in `Run'
What's missing? Thanks!
assert() is an instance method on TestCase so would only be available to instances of Tests. You are calling it inside a class method so Ruby is looking for a class method in Tests which doesn't exist.
A better way to do this is to make Tests a module and the Run method an instance method:
module Tests
def Run(browser)
...
end
end
Then include the Tests module in your test class:
class TwoSCMCrossBranch < Test::Unit::TestCase
include Tests
def test_two_scm_cross_branch
test_site = Constants.whatsInUrl
puts " Step 1: go to the test site: " + test_site
ie = Watir::IE.start(test_site)
Run(ie)
end
end
which will make the Run method available to the test and Run() will find the assert() method in the test class.
It might be worth a try to remove the asserts all together, and just use .exists?.

Resources