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 3607040
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:19:33+00:00 2026-05-18T21:19:33+00:00

So I have this reaaaally long method that I want to test. I could

  • 0

So I have this reaaaally long method that I want to test. I could probably break it some other 4-5 methods, but I don’t want to make those methods public (and although I could, I’d like to stay away, if possible, from turning them into package-protected/internal):

If my math isn’t wrong, to test this as it is I’ll need something like 8 tests. Dividing this in smaller methods wouldn’t make me have to make less unit-tests, would it?
The big problem I see with creating smaller methods(for testing) besides the visility issue is the need to pass to those small methods lots of arguments.

Although it was me who originally wrote this code, I still find it hard to understand.
So my question is…how to test this?!? How would you do it? Should I restructure this in any way?

    public Genoma GenerateOffspring(Genoma genoma1, Genoma genoma2) {
        if (genoma1.NumberOfGenes != genoma2.NumberOfGenes) throw new ArgumentException("Both individuals must have the same size!");
        if (genoma1.NumberOfGenes == 0 || genoma2.NumberOfGenes == 0) throw new ArgumentException("0-sized genomas not allowed!");

        if (genoma1.NumberOfGenes == 1) {
            if (numberGenerator.GenerateInteger(0, 2) == 0) {
                return genoma1;
            } else {
                return genoma2;
            }
        }

        int cutPoint = numberGenerator.GenerateInteger(1, genoma1.NumberOfGenes);

        Genoma parent1;
        Genoma parent2;

        if (numberGenerator.GenerateBool()) {
            parent1 = genoma1;
            parent2 = genoma2;
        } else {
            parent1 = genoma2;
            parent2 = genoma1;
        }

        Genoma offspring = parent1.Clone();

        if (numberGenerator.GenerateBool()) {
            for (int i = 0; i < cutPoint; ++i) {
                offspring.SetGeneAt(i, parent2.GetGeneAt(i));
            }
        } else {
            for (int i = cutPoint; i < offspring.NumberOfGenes; ++i) {
                offspring.SetGeneAt(i, parent2.GetGeneAt(i));
            }
        }

        return offspring;
    }

Thanks

  • 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-05-18T21:19:33+00:00Added an answer on May 18, 2026 at 9:19 pm

    The main thing to keep in mind is, when you start testing, you will run all tests, but for a given test, if it fails one assertion at some point, it will stop, meaning that what is after it will not be tested because it is assumed to require what was already not correctly asserted.

    The good thing about dividing it into smaller methods would be that you would end up getting more things that could be tested independently, but the increase in arguments that you mention suggests that it would not be a “natural” thing to do, so you should keep it all in same test because you are, according to your words, seeing the whole function as a unit itself, and so, one unit test should test that unit.

    Additionally, the increase in arguments would also make your code less pretty, and less efficient, I think.

    I hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might sound like a reaaaally dumb question but... why do browsers have a
I have this code in jQuery, that I want to reimplement with the prototype
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do
I have this method: -(NSString *)scrambleWordGenerator: (NSUInteger)length { NSMutableString *scrambledWord = [[NSMutableString alloc] initWithString:@];
I have this method (someone else wrote it!) - (IBAction)showMasterPopover:(id)sender { if (_hiddenPopoverController &&
I have this webpage which shows me some images and some images are on
I have this code but it doesn't work! public class Trial { public static
I have this div which acts a lens in zooming of the image. But
Have this upload script, and it works. But I would like to add a

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.