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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:53:26+00:00 2026-06-04T02:53:26+00:00

Specifically I am worried about this scenario : 1) I write a class with

  • 0

Specifically I am worried about this scenario :

1) I write a class with 1 member variable and a copy function

class MyClass
{
private:
int grade;

public:
void setGrade(int x) { grade = x; }
int getGrade() { return grade; }
void copyFrom(const MyClass* src) { grade = src->grade; }
}

2) Write a unit test along these lines

void testCopyFrom()
{
MyClass mc1;
MyClass mc2;
mc1.setGrade(-347);
mc2.setGrade(10);
mc2.copyFrom(&mc1);
TS_ASSERT_EQUALS(mc2.getGrade(), -347);
}

3) After a couple months I add a second member variable

private:
int grade, extra;

and, (you guessed it !) forget to update the copyFrom() function accordingly.

In this case the unit test will falsely report success. Have I done something wrong with the testing design?

  • 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-04T02:53:28+00:00Added an answer on June 4, 2026 at 2:53 am

    TDD means test first: before you change the code, you should update your code. In this case, if you decide to add extra, you should be adding tests for the API around extra (so far you have none, but maybe you’ll want getExtra() et al.), and then, as you add and revise tests, you’ll notice that you need to add setExtra() and getExtra() calls to testCopyFrom().

    At this stage your tests will mostly be failing (red), and you can then do the real code to make them pass (green), and finally, clean up and refactor, using your tests as a guide as to whether you’ve broken anything.

    Read more about red-green-refactor—it is TDD.

    Also, you should mark getGrade() as const, i.e.

    int getGrade() const { return grade; }
    

    This means you can use a const pointer or reference to a MyClass instance to get the grade.

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

Sidebar

Related Questions

Specifically, I am talking about this code: $(window).scroll(function(){ var loc = $('html').offset().top; }); To
Specifically, why would a variable declared in both a parent and child class be
Specifically, I'd like to write the Windows Media Player frames, such as the WM/MediaClassPrimaryID
Specifically, I want to write a powershell script to find every file with extensions.lib
Specifically (taking a deep breath): How would you go about finding all the XML
I'm reading this article on how to : correctly retain variable state in Android
Specifically I am looking for a good OOP PHP CURL class that can be
Specifically: what library is MockObjectTestCase in? I'm following this link: http://therning.org/niklas/2005/05/jmock-solid-testing/ Is there an
Specifically, I need to validate the incoming X.509 security certificate against a database to
Specifically I am interested for the LAMP Stack and some of the best practices

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.