0

How a unit test can be made for this kind of structure?
How to change the call so function a() will call function mock_b() instead of function b().

// source.h
int b(int x);
bool a();


// source.cpp
int b(int x){return x;}
bool a(){return (b(5) > 0);}


// tester.cpp
#include "source.h"
int mock_b(int x){return -8;}

unit_test(){
    assert(true == a());
    b = mock_b;                 //How this replacement can be done?
    assert(false == a());
}
2
  • It is complicated, as your design is not good for separate tests. -- Don't test functions or methods separately, test features. -- You might want to search this site for duplicates of your question, unfortunately I don't have the time right now. Commented Jun 15, 2022 at 20:27
  • Generally you can do #define b mock_b. Commented Jun 17, 2022 at 6:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.