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

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?.

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?

Using Capybara within a Ruby class

I'm just experimenting a little with Cucumber and Capybara.
I'm writing a class that will perform some user admin for me.
I have the following class:
class UserAdmin
def initialize(data)
#data = data
end
def add_user
require 'rspec/expectations'
require 'capybara/cucumber'
require 'capybara/helpers'
#data.hashes.each do |user_details|
load_user_data(user_details)
fill_in('firstname', with: #first_name)
fill_in('surname', with: #last_name)
fill_in('username', with: #new_username)
fill_in('usernameConfirmation', with: #confirm_new_username)
click_button_add_user
end
end
When I try and create an instance of this class, I get `NoMethodError: undefined method fill_in' for #
I thought by requieing Capybara etc, I could use their methods in my class.
Clearly I'm wrong, could anyone point out where I've gone wrong please?
You should include Capybara::DSL:
require 'capybara/dsl'
class UserAdmin
include Capybara::DSL
Capybara.run_server = false
# ...
end

Why 'undefined method `assert_equal' ' is thrown even after requiring 'test/unit'

I opened irb & entered:
require 'test/unit'
but when I used the assert_equal method, I got following error: NoMethodError: undefined method 'assert_equal' for main:Object. Why is this happening even after requiring 'test/unit' ?
assert_equal is defined on subclasses of Test::Unit::TestCase, so are only available in that class. You may have some success with include Test::Unit::TestCase to load those methods onto the current scope.
More likely you could be better writing your tests in a short file, and running them with ruby ./my_file.rb
You can use in built ruby error testing
raise "Message you want to throw when error happens" if/unless "Condition when you want to throw the error "
OR
If you get error messages when trying to use assertions, like "NoMethodError: undefined method `assert' for main:Object", then add this to the top of your script:
require "test/unit/assertions"
include Test::Unit::Assertions
This is how assertions are used:
class Gum
def crisis; -42 end
end
# and as for testing:
require 'test/unit'
class GumTest < Test::Unit::TestCase
def test_crisis
g = Gum.new
assert_equal -42, g.crisis
end
end

With Test::Unit, how can I run a bit of code before all tests (but not each test)?

In my test app, which uses test::unit, I need to start by pulling a bunch of data from various sources. I'd like to only do this once - the data is only read, not written, and doesn't change between tests, and the loading (and error checking for the loading), takes some time.
There are values that I DO want reset every time, and those are easy enough, but what if I want persistant accessible values? What's the best way to do this?
I'm especially interested in solutions that would let my push those assignments to some module that can be included in all my tests, since they all need access to this data.
Why do you need it inside the test? You could define it gloabl:
gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
GLOBAL_DATA = 11
class My_Tests < Test::Unit::TestCase
def test_1()
puts "Testing startup 1"
assert_equal(11, GLOBAL_DATA)
end
end
GLOBAL_DATA could be a (singleton)-class (respective an instance).
If you have only one testclass, you may use TestCase.startup:
gem 'test-unit'#, '>= 2.1.1' #startup
require 'test/unit'
class My_Tests < Test::Unit::TestCase
def self.startup
puts "Define global_data "
##global_data = 11
end
def test_1()
puts "Testing 1"
assert_equal(11, ##global_data = 11)
end
def test_2()
puts "Testing 2"
assert_equal(11, ##global_data = 11)
end
end
You can just put them at the top of the class. They will get executed, and then your tests will get executed.
You could do this in the setup method:
def setup
if !defined?(##initial_data)
# Whatever you need to do to get your initial data
##initial_data = foo
end
#other_data = bar
end

Webrat Mechanize outside of Rails

I'm trying to use Webrat in a standalone script to automate some web browsing. How do I get the assert_contain method to work?
require 'rubygems'
require 'webrat'
include Webrat::Methods
include Webrat::Matchers
Webrat.configure do |config|
config.mode = :mechanize
end
visit 'http://gmail.com'
assert_contain 'Welcome to Gmail'
I get this error
/usr/lib/ruby/gems/1.8/gems/webrat-0.6.0/lib/webrat/core/matchers/have_content.rb:57:in 'assert_contain': undefined method assert' for #<Object:0xb7e01958> (NoMethodError)
assert_contain and other assertions are methods of test/unit, try to require it and use webrat from inside a test method:
require 'test/unit'
class TC_MyTest < Test::Unit::TestCase
def test_fail
assert(false, 'Assertion was false.')
end
end
anyway i haven't tested it but I have a working spec_helper for rspec if this can interest you:
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
require 'spec/rails'
require "webrat"
Webrat.configure do |config|
config.mode = :rails
end
module Spec::Rails::Example
class IntegrationExampleGroup < ActionController::IntegrationTest
def initialize(defined_description, options={}, &implementation)
defined_description.instance_eval do
def to_s
self
end
end
super(defined_description)
end
Spec::Example::ExampleGroupFactory.register(:integration, self)
end
end
plus a spec:
# remember to require the spec helper
describe "Your Context" do
it "should GET /url" do
visit "/url"
body.should =~ /some text/
end
end
give it a try I found it very useful (more than cucumber and the other vegetables around) when there is no need to Text specs (features) instead of Code specs, that I like the most.
ps you need the rspec gem and it installs the 'spec' command to execute your specs.

Resources