Ruby undefined method for 'assert' for :class - ruby

I'm trying to use the Test::Unit::TestCase API in ruby to do some unit tests but have run into an issue with using assert. I am only trying to call specific methods within a class that uses the Test::Unit::TestCase class but it keeps failing on assert. In my rake file I have:
require 'test_file'
task :manage do
my_app = Test::Unit::TestCase::Unit_Test
my_app.test1
end
And in my test_file.rb I have:
require 'rubygems'
require 'test/unit'
require 'rack/test'
class Unit_Test < Test::Unit::TestCase
include Rack::Test::Methods
# manage tests
def self.test1
browser = Rack::Test::Session.new(Rack::MockSession.new(Sinatra::Application))
browser.get '/homepage'
assert browser.last_response.ok?
end
end
Everything works up until I get to the 'assert' statement which says: Undefined method assert for Unit_Test:Class. If I do not make the method a static method than it will run ALL of the methods within the Unit_Test class. I only want to run specific unit tests from my rake file.
What am I missing?

I can't think of a nice way to use assert from a class method because assert is an instance method.
If you want to run just a specific test method you might be better off making the test an instance method and using the name parameter, e.g.:
ruby test_file.rb --name test1
You should be able to invoke that from your rake task.

Related

Unable to call Ruby mixin instance method from RSpec

Objective:
I want to run a basic RSpec unit test on an instance method of a mixin (module) named Debug. Below are the file contents of the Debug mixin:
Mixin File: ./mixins/debug.rb
module Debug
public
def class_info?
"#{self.class.name}"
end
end
Validate Debug mixin instance methods accessible from RSpec:
When I run irb and include the Debug mixin with commands require_relative './mixins/debug.rb' and include Debug, and then call Debug.class_info? it successfully returns "Module"
Then if I run rspec with the following RSpec unit test to confirm that the RSpec context can access the instance methods of the mixin, the test successfully passes:
RSpec Unit Test Setup #1: ./spec/mixins/debug_spec.rb
require_relative '../../mixins/debug.rb'
RSpec.describe Debug, "#class_info?" do
include Debug
before(:each) do
#class_info_instance_method = Debug.instance_methods[0].to_s
end
context "with mixins" do
it "has class info instance method" do
expect(#class_info_instance_method).to eq "class_info?"
end
end
end
Problem when calling Debug mixin instance method from RSpec:
Lastly, I change the RSpec unit test to be as follows, so instead it actually calls the class_info? instance method of the Debug mixin:
RSpec Unit Test Setup #2: ./spec/mixins/debug_spec.rb
require_relative '../../mixins/debug.rb'
RSpec.describe Debug, "#class_info?" do
include Debug
before(:each) do
#class_info = Debug.class_info?
end
context "with mixins" do
it "shows class info" do
expect(#class_info).to eq "Module"
end
end
end
But now when I run rspec from the command line, why does it return the following error? (Note: even though in the previous RSpec Unit Test Setup #1 that was entirely similar I checked I could successfully access this Debug mixin instance method)
1) Debug#class_info? with mixins shows class info
Failure/Error: #class_info = Debug.class_info?
NoMethodError:
undefined method `class_info?' for Debug:Module
Note: I have shared the above code in my RubyTest GitHub repo.
Setup and References:
My System:
Ruby: ruby 2.3.0p0 (ruby -v)
RSpec: 3.5.4 (rspec -v)
References:
Applying example from Mixins chapter of Programming Ruby book
When you include a module, the methods become instance methods in the included class. Debug.class_info? doesn't work because there is no class method class_info?. I'm also not sure that the way you've included the module in your test is the best way to do it. Would something like this work?
require_relative '../../mixins/debug.rb'
class TestClass
include Debug
end
RSpec.describe Debug, "#class_info?" do
let(:test_instance) { TestClass.new }
context "with mixins" do
it "shows class info" do
expect(test_instance.class_info?).to eq "TestClass"
end
end
end

ruby unit testing at_start not found

I am using test-unit-2.5.5 with ruby 1.9.3. In http://test-unit.rubyforge.org/test-unit/en/Test/Unit.html#at_start-class_method there is a method called at_start as part of the ruby test::unit module from version 2.5.2. I tried to use it from the examples on the page like so:
class TestAOS < Test::Unit::TestCase
Test::Unit.at_start do
puts "start"
end
Test::Unit.at_exit do
puts "Exit!"
end
But when I run my test I get the following:
NoMethodError: undefined method `at_start' for Test::Unit:Module
TestAOS at unit/TestAOS.rb:8
(root) at unit/TestAOS.rb:7
Do I need to do anything first before this method can be used? I'm new to ruby
When I comment out the at_start bloack and run the test I get a different error for at_exit:
NoMethodError: private method `at_exit' called for Test::Unit:Module
TestAOS at unit/TestAOS.rb:12
(root) at unit/TestAOS.rb:7
A
In the example provided by your link the
Test::Unit.at_start do
puts "start"
end
is called outside of test class. You are calling it from inside of your test class. Just move it outside of your TestAOS

Sinatra-Contrib: undefined method `namespace'

I'm new to TDD, and I'm trying to write methods to generate URLs on a per-class basis that inherit from a parent class using Sinatra and the Namespace library in Sinatra-Contrib. I'm not too far along, because I've gotten a failed RSpec test that has kept me from moving further: undefined method 'namespace'. Can anyone help?
Gemfile:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'rack'
gem 'thin'
group :development, :test do
gem 'rspec'
gem 'rack-test'
gem 'ZenTest'
gem 'autotest-growl'
gem 'autotest-fsevent'
end
base_model.rb:
require 'sinatra'
require 'sinatra/namespace'
require 'rack'
def generate_routes_for_model(model_class, rootUrl)
namespace rootUrl do
get '/show' do
"I'm the model's show route"
end
end
end
base_model_spec.rb
require_relative '../base_model.rb'
require 'rack/test'
set :environment, :test
class Foo
end
def app
Sinatra::Application
end
include Rack::Test::Methods
describe 'Create Routes for test Class' do
it "should load foo.show" do
generate_routes_for_model(Foo, '/foo')
get '/foo/show'
last_response.should be_ok
end
end
Here's the result of the test:
Failures:
1) Create Routes for test Class should load foo.show
Failure/Error: generate_routes_for_model(Foo, '/foo')
NoMethodError:
undefined method `namespace' for #<RSpec::Core::ExampleGroup::Nested_2:0x007f8571102e00>
# ./base_model.rb:16:in `generate_routes_for_model'
# ./spec/base_model_spec.rb:24:in `block (2 levels) in <top (required)>'
It appears to be a scope problem, on first look. namespace is a class method of a Sinatra extension (which is why you call it within the class definition and not inside an instance method).
When you run RSpec, is namespace ever within a class that inherits from Sinatra::Base? I don't think so. You need to provide a Sinatra class that namespace can run within. e.g.
class Foo; end
app = Sinatra.new do
register Sinatra::Namespace
end
def generate_routes_for_model(app_class, model_class, rootUrl)
['show'].each do |route|
app_class.namespace rootUrl do
get route do
"I'm the model's show route"
end
end
end
end
generate_routes_for_model app, Foo, "/"
app.run!
Then going to http://localhost:4567/show will respond with "I'm the model's show route".
Or perhaps use class_eval to define the code, there's a few ways you could tackle this, main thing to keep in mind is make sure the scope is right or the app or RSpec etc won't be able to find the method.
Edit:
Also, I'd move the generation into a before block, as it's set up for an integration test (you're essentially saying "If the route exists then the generation worked, I don't need to test the method here", so it's a black box test of the public API - an integration test by any other name:) If you wanted to test the method itself that would be a unit test, and you'd need to look into the app to see the routes had been generated.
describe 'Create Routes for test Class' do
before do
generate_routes_for_model(Foo, '/foo')
get '/foo/show' # this, too, is not a test but a pre-requisite for the test
end
it "should load foo.show" do
last_response.should be_ok
end
end

How to use omit for Test::Unit::TestCase in Ruby to omit/skip the execution of a certain method/test

I am trying to skip/omit some tests. My environment is Ruby/selenium webdriver/Test Unit.
This how the structure looks:
class HeadTestCase < Test::Unit::TestCase
class UnitTest1 < CCTestCase
class UnitTest1 < CCTestCase
def web_test_01
omit("Skipping this test")
some code related to test
end
Omit method signature is omit("Message",&block)
I dont know what I should put for &block.
Also is this a correct way to skip a test?
Using omit the way you are using it here looks correct. The trick is that you need to be using the test-unit gem and not the minimal version of Test::Unit included with the Ruby distribution.
gem install test-unit
You do not need to use a code block with omit unless you want only part of the code in your test case to be omitted. Use do and end as usual for a block:
def web_test_01
omit("Skipping part of the test") do
# Code here would be skipped
end
# Code here would be executed
end

Test::Unit::TestCase from rakefile?

I'm stuck trying to call individual unit test methods from a rake file to automate my testing but I keep getting an error. Every time I run 'rake manage' I get an error in my 'manage' task saying: wrong number of arguments 0 for 1. Here is my rake file:
require "test_file"
task :default => [:commands]
task :manage do
myTest = Unit_Test.new
myTest.test
end
And my actual class that has uses the Test::Unit::TestCase class. This is in a separate file called 'test_file.rb'.
class Unit_Test < Test::Unit::TestCase
include Rack::Test::Methods
def test
puts "this is just a test"
end
end
There error is pointing to:
myTest = Unit_Test.new
How do I call individual methods from this class? I basically want to call certain methods from this class in different tasks but I cannot get it to work. How do I get this working?
Have you considered doing
ruby test_file.rb --name test_method_to_be_run
Or do you need to run multiple test methods?
Also, what version of Ruby are you using? 1.8 or 1.9?

Resources