Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9312867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:44:16+00:00 2026-06-19T01:44:16+00:00

When I’m doing unit testing for code that I don’t trust very much, I’ll

  • 0

When I’m doing unit testing for code that I don’t trust very much, I’ll commonly use this pattern:

  1. Think up many (perhaps dozens) of expectations I have for the output of my function (I think of these as “theories” about how the code works).
  2. Spin up many thousands of objects.
  3. Run each object through the dozens of assertions I’ve coded up reflecting my expectations of how the code works.

In Ruby’s Test::Unit (to which I am new), I’ve been doing something like this:

class TestFooBarrer < Test::Unit::TestCase
  def setup
    @lots_of_objects_to_be_tested = FooBarrer.bar_lots_of_foos
  end

  def assert_foo_has_been_barred(foo)
    assert_kind_of Bar, foo
  end

  def assert_foo_has_baz_method(foo)
    assert_respond_to foo, :baz
  end

  #Many more assertions along those lines go here.

  def test_all_theories
    @lots_of_objects_to_be_tested.each do |foo|
      assert_foo_has_been_barred(foo)
      assert_foo_has_baz_method(foo)
      # etc.
      #
    end
  end
end

This obviously gets a bit unwieldy when the number of theories I’m testing is in the dozens, and involves what looks to me like a lot of needless repetition. I’d prefer to do something like this:

class FooTheories
  def self.assert_all_theories(foo)
    # ???
  end

  def assert_foo_has_been_barred(foo)
    assert_kind_of Bar, foo
  end

  def assert_foo_has_baz_method(foo)
    assert_respond_to foo, :baz
  end

  #Many more assertions along those lines go here.
end


class TestFooBarrer < Test::Unit::TestCase
  def setup
    @lots_of_objects_to_be_tested = FooBarrer.bar_lots_of_foos
  end

  def test_all_theories
    @lots_of_objects_to_be_tested.each do |foo|
      FooTheories.assert_all_theories(foo)
    end
  end
end

Basically, I’m looking for a way to write a bunch of assertions in one place and then call them over and over again on a large quantity of objects.

Is there any support for something like that in Ruby? I’m not tied to Test::Unit particularly. Any of the testing frameworks is fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:44:17+00:00Added an answer on June 19, 2026 at 1:44 am

    What I would do is generate tests on the fly. Add a method in your test_helper:

    def test(obj, &block)
      define_method("test_#{ obj.to_s }", &block)
    end
    

    And then you can make your test suite like the following

    class TestObjects < Test::Unit::TestCase
    
      @objects_to_test = [...]
    
      @objects_to_test.each do |obj|
        test obj do
    
          assert obj.is_a?(Foo), 'not a foo'
    
          # all assertions here
    
        end
      end
    
    end
    

    And then if it fails, you’ll get to know which object failed because the name of the test is the string representation of the object. Ex message:

    1) Failure:
    test_#<Bar:0x000001009ee930>(TestObjects):
    not a foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am confused How to use looping for Json response Array in another Array.
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.