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

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

Related

Can't call module method

I'm writing a Chef cookbook to deploy and application and create users. It doesn't have an API, and uses an odd hashing method, so I've written a short library module for it. I've included only the makeSalt() method below for the sake of brevity.
module Foo_packagist
module Password
def makeSalt(len=31)
require 'securerandom'
return Digest.hexencode(SecureRandom.random_bytes((len*6/8.0).ceil)).to_i(16).to_s(36)[0..len-1]
end
end
end
The trouble is that in every Chef run I get:
NoMethodError
-------------
undefined method `makeSalt' for Foo_packagist::Password:Module
and debugging in chef-shell I get:
chef (12.4.0)> puts ::Foo_packagist::Password.instance_methods()
makeSalt
encodePassword
chef (12.4.0)> puts ::Foo_packagist::Password.makeSalt()
NoMethodError: undefined method `makeSalt' for Foo_packagist::Password:Module
chef (12.4.0)> puts ::Foo_packagist::Password::makeSalt()
NoMethodError: undefined method `makeSalt' for Foo_packagist::Password:Module
What is the right way to call this method?
Change that to def self.makeSalt. That's the Ruby syntax for a module-level method.
Try this ->
module Foo_packagist
module Password
def self.makeSalt(len=31)
require 'securerandom'
return Digest.hexencode(SecureRandom.random_bytes((len*6/8.0).ceil)).to_i(16).to_s(36)[0..len-1]
end
end
end
Then to call it would be this->
Foo_packagist::Password.makeSalt()

No error when running unit test with an expectation on method calls (Ruby/Test::Unit/Mocha)

I have a question regaring Mocha expectations running with the Ruby Test::Unit framework.
My purpose is to verify the methods called when I call a wrapping method.
This test always seem to pass, I don't understand why...
I am expecting an error stating that the method clean has only be called once (with can be verified in the Expectation object).
Why is the following Unit Test not raising an error when I run the following test:
require 'test/unit'
require 'mocha'
require 'mocha/test_unit'
class DoMe
def stop_and_clean
clean
stop
end
def clean
true
end
def stop
true
end
end
class DoMeTest < Test::Unit::TestCase
def test_stop_and_clean
d = DoMe.new
d.expects(:clean).times(5)
assert(d.stop_and_clean)
end
end
It seems to me that expects(:clean).times(5) will not be satisfied, but running the test shows no error. Do I need to make an assertion on the Expectation object ?
exp = d.expects(:clean).times(5)
[...]
assert(that exp is invoked only once)

Why do I get the error uninitialized constant Stuff::HTTParty?

I have the HTTParty gem on my system and I can use it from within rails.
Now I want to use it standalone.
I am trying:
class Stuff
include HTTParty
def self.y
HTTParty.get('http://www.google.com')
end
end
Stuff.y
but I get
$ ruby test_httparty.rb
test_httparty.rb:2:in `<class:Stuff>': uninitialized constant Stuff::HTTParty (NameError)
from test_httparty.rb:1:in `<main>'
07:46:52 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker 73845718_get_method
$
You have to require 'httparty':
require 'httparty'
class Stuff
include HTTParty
# ...
end
Its all because of the include which exists with in the class
If you include a class with a module, that means you're "bringing in" the module's methods as instance methods.
If you need more clarity on include and require
I request you to refer to this wonderful SO Posting
What is the difference between include and require in Ruby?
Here is an example which I have taken from the same posting
module A
def say
puts "this is module A"
end
end
class B
include A
end
class C
extend A
end
B.say => undefined method 'say' for B:Class
B.new.say => this is module A
C.say => this is module A
C.new.say => undefined method 'say' for C:Class

Testing rack-timeout in sinatra and ruby

This is something that I thought would be straightforward but I'm having issues around testing the rack-timeout gem. I have a sinatra base class with an endpoint which does some logic.
module MyModule
class MySinatra < Sinatra::Base
use Rack::Timeout
Rack::Timeout.timeout = 10
get '/dosomething' do
#do the normal logic.
end
end
end
More information on the rack-timeout gem is here. I'm trying to setup a test where I can send a request which I know will take more than a few seconds in order for it to fail.
Here is the test so far
require "test/unit"
require "mocha/setup"
require 'rack/timeout'
def test_rack_timeout_should_throw_timed_out_exception_test
Rack::Timeout.stubs(:timeout).returns(0.0001)
assert_raises TimeoutError do
get "/dosomething"
end
Rack::Timeout.unstub
end
There are a number of ways this could be done but I am not sure how they would be implemented
Override the '/dosomething' method as part of the test to {sleep 3}
Do the same as above but with a stubbing or mocking library
instead of using get "/dosomething" in the test, create a net::http response which will keep the request open.
Any thoughts on this would be very much appreciated.
First of all your test will not actually pass, because the error is not handed through to the test. It is only raised on the server side. Luckily, rack-test provides the last_response.errors method to check whether there were errors. Therefore i would write the above test as follows:
def test_rack_timeout_should_throw_timed_out_exception
Rack::Timeout.stubs(:timeout).returns(0.0001)
get '/dosomething'
assert last_response.server_error?, 'There was no server error'
assert last_response.errors.include?('Timeout::Error'), 'No Timeout::Error raised'
Rack::Timeout.unstub
end
Now the only thing left to do is to simulate a slow response by overriding the route. It seemed simple at first but then i realized it is not so simple at all when i got my hands on it. I fiddled around a lot and came up with this here:
class Sinatra::Base
def self.with_fake_route method, route, body
old_routes = routes.dup
routes.clear
self.send(method.to_sym, route.to_s, &body.to_proc)
yield
routes.merge! old_routes
end
end
It will allow you to temporarily use only a route, within the block you pass to the method. For example now you can simulate a slow response with:
MyModule::MySinatra.with_fake_route(:get, '/dosomething', ->{ sleep 0.0002 }) do
get '/dosomething'
end
Note that the get '/dosomething' inside the block is not the definition of the temporary route, but a method of rack-test firing a mock request. The actual override route is specified in form of arguments to with_route.
This is the best solution i could come up with but i would love to see a more elegant way to solve this.
Complete working example (ran on Ruby 1.9.3.p385):
require 'sinatra/base'
require 'rack/timeout'
module MyModule
class MySinatra < Sinatra::Base
use Rack::Timeout
Rack::Timeout.timeout = 10
get '/dosomething' do
'foo'
end
end
end
require 'test/unit'
require 'rack/test'
require 'mocha/setup'
class Sinatra::Base
def self.with_fake_route method, route, body
old_routes = routes.dup
routes.clear
self.send(method.to_sym, route, &body)
yield
routes.merge! old_routes
end
end
class Tests < Test::Unit::TestCase
include Rack::Test::Methods
def app
MyModule::MySinatra
end
def test_rack_timeout_should_throw_timed_out_exception
Rack::Timeout.stubs(:timeout).returns(0.0001)
MyModule::MySinatra.with_fake_route(:get, '/dosomething', ->{ sleep 0.0002 }) do
get '/dosomething'
end
assert last_response.server_error?, 'There was no server error'
assert last_response.errors.include?('Timeout::Error'), 'No Timeout::Error raised'
Rack::Timeout.unstub
end
end
produces:
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips

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