Click here to Skip to main content
15,895,538 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: ShowNotifyBaloon problem Pin
_Flaviu7-Sep-11 7:15
_Flaviu7-Sep-11 7:15 
GeneralRe: ShowNotifyBaloon problem Pin
Chuck O'Toole7-Sep-11 13:05
Chuck O'Toole7-Sep-11 13:05 
Question"The system cannot find the file specified" Pin
AndrewG12312-Sep-11 12:07
AndrewG12312-Sep-11 12:07 
AnswerRe: "The system cannot find the file specified" Pin
Cheongwadae2-Sep-11 17:34
Cheongwadae2-Sep-11 17:34 
QuestionIntroducing tests into a legacy application? Pin
Stefan_Lang1-Sep-11 22:18
Stefan_Lang1-Sep-11 22:18 
AnswerRe: Introducing tests into a legacy application? Pin
Orjan Westin1-Sep-11 23:10
professionalOrjan Westin1-Sep-11 23:10 
GeneralRe: Introducing tests into a legacy application? Pin
Stefan_Lang1-Sep-11 23:59
Stefan_Lang1-Sep-11 23:59 
GeneralRe: Introducing tests into a legacy application? Pin
Orjan Westin2-Sep-11 3:30
professionalOrjan Westin2-Sep-11 3:30 
Mocking isn't always necessary - it depends on the units you are testing. If they conform to the classic design ideals - low coupling, small interface, high cohesion - the lion's share of their function should be testable with no mock objects. But seeing such designs in real life, especially in old production apps, is quite rare. Frown | :(

A mockup is only needed if the unit you want to test is dependent on something that is expensive or impossible to provide in a simple test.

For instance, a self-contained unit that calculates income tax probably wouldn't need any mock objects.

C++
// Header file with interface of unit to test
namespace taxCalc
{
  double calcIncomeTax(double annualIncome);
  double calcIncomeTax(double annualIncome, double pensionDeduction);
...
}

// Hand-rolled unit test file
void test_taxCalc()
{
  std::cout << "Testing taxCalc::calcIncomeTax" << std::endl;  
  int passed = 0;
  int tests = 4;
  if (15.3 == calcIncomeTax(60.0))
    ++passed;
  if (0 == calcIncomeTax(5.0))
    ++passed;
  if (0 == calcIncomeTax(0.0))
    ++passed;
  if (14.6 == calcIncomeTax(60.0, 4.0))
    ++passed;
  std::cout << passed << " out of " << tests << " passed" << std::endl;
  if (passed != tests)
    std::cout << "There were errors" << std::endl;
}

int main()
{
  test_taxCalc(); 
  return 0;
}


There you go, a very simple unit test. As you see, there can be multiple tests in one function.

If you had a second unit in the system, to calculate national insurance or something, you can test that in the same test file, though it makes sense to write a separate test function for it.

Now, it could be that for hysterical reasons a simple function is dependent on something hugely expensive, like access to a corporate database with lots of security and whatnot. That's when you make a mock. Not of what you're testing, but of what the tested unit is dependent on.

C++
// Expensive object interface
class DataBaseTableFromFortKnox
{
public:
  DataBaseTableFromFortKnox();
  // Lots of fancy stuff and set-up and keys and blood of firstborn
  ...
  // The one function the unit we're testing actually uses
  double GetCentsPerDollar() const;
  ...
};

// Header file with interface of unit to test
namespace taxCalc
{
  double calcIncomeTax(double annualIncome);
  double calcIncomeTax(double annualIncome, double pensionDeduction);
  double calcIncomeTaxInCents(double annualIncome, const DataBaseTableFromFortKnox& dbTab);
...
}

// Our fancy mock
class DataBaseTableFromFortKnox
{
public:
  // Make stub verions of everything
  DataBaseTableFromFortKnox()
  {}
  ...
  // The one function the unit we're testing actually uses
  double GetCentsPerDollar() const
  {
    return 100.0;
  }
};

// Hand-rolled unit test file
void test_taxCalc()
{
...
}

void test_taxCalcDB()
{
  // Make db table
  DataBaseTableFromFortKnox db;
  
  std::cout << "Testing taxCalc::calcIncomeTax with mock DB connection" << std::endl;  
  int passed = 0;
  int tests = 4;
  if (1530.0 == calcIncomeTax(60.0, db))
    ++passed;
...


In this case, you must make sure that you link to the mock rather than the real thing.

In one of the projects I'm managing where I work, there's a DLL that's dependent on some libs from other departments. For that project, I have two unit test projects - one where I've replaced the other libs with stub libs, and one where it's built with the proper libs. The first tests the internal logic of the DLL, making sure that the correct libs are called for the various inputs. Only when that test project runs successfully do I need to run the other, which is calling into the other libs (which takes a few minutes to initialise and close down).
GeneralRe: Introducing tests into a legacy application? Pin
Stefan_Lang2-Sep-11 4:01
Stefan_Lang2-Sep-11 4:01 
GeneralRe: Introducing tests into a legacy application? Pin
Orjan Westin2-Sep-11 3:45
professionalOrjan Westin2-Sep-11 3:45 
QuestionText drawing is not getting properly Pin
Amrit Agr31-Aug-11 3:15
Amrit Agr31-Aug-11 3:15 
AnswerRe: Text drawing is not getting properly Pin
Alan Balkany31-Aug-11 4:10
Alan Balkany31-Aug-11 4:10 
AnswerRe: Text drawing is not getting properly Pin
xrg_soft@163.com31-Aug-11 6:16
xrg_soft@163.com31-Aug-11 6:16 
GeneralRe: Text drawing is not getting properly Pin
Amrit Agr1-Sep-11 22:50
Amrit Agr1-Sep-11 22:50 
GeneralRe: Text drawing is not getting properly Pin
xrg_soft@163.com5-Sep-11 18:13
xrg_soft@163.com5-Sep-11 18:13 
Questionconvert a C app to an MFC based Windows app Pin
Angela201231-Aug-11 3:00
Angela201231-Aug-11 3:00 
QuestionHow to learn C++ Pin
shanmugarajaa30-Aug-11 18:16
shanmugarajaa30-Aug-11 18:16 
QuestionRe: How to learn C++ Pin
David Crow31-Aug-11 2:41
David Crow31-Aug-11 2:41 
AnswerRe: How to learn C++ Pin
Chris Losinger31-Aug-11 3:02
professionalChris Losinger31-Aug-11 3:02 
GeneralRe: How to learn C++ Pin
Albert Holguin31-Aug-11 8:22
professionalAlbert Holguin31-Aug-11 8:22 
AnswerRe: How to learn C++ Pin
Nemanja Trifunovic31-Aug-11 6:10
Nemanja Trifunovic31-Aug-11 6:10 
GeneralRe: How to learn C++ Pin
Albert Holguin31-Aug-11 8:25
professionalAlbert Holguin31-Aug-11 8:25 
GeneralRe: How to learn C++ Pin
Orjan Westin31-Aug-11 23:37
professionalOrjan Westin31-Aug-11 23:37 
AnswerRe: How to learn C++ Pin
MMJ19892-Sep-11 22:28
MMJ19892-Sep-11 22:28 
QuestionHow to get software (exe, dll) Version info from a buffer? Pin
includeh1030-Aug-11 16:16
includeh1030-Aug-11 16:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.