Kevin
Kevin

Reputation: 2888

Ruby undefined method for 'assert' for :class

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?

Upvotes: 1

Views: 1500

Answers (1)

Steve
Steve

Reputation: 15736

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.

Upvotes: 1

Related Questions