C ++: how to simulate different time intervals in unit tests using google test

I am working in C ++ with the Google unit test. There is still a problem in our company code that system data types, such as CTime, use, for example, the time zone for a real system (we use the CTime data type in the same way as in production code). For unit testing, we want to model different time zones, because we have a lot of problems with this.

I have already invested hours, researching how others do it, because it cannot be that other companies do not have this problem :) But I did not find a solution for this. My thoughts are: CTime itself saves time in UTC and converts it on demand into local time. My guess is that all these functions require the GetTimeZoneInformation () function, which is declared in the timestamp .h.

WINBASEAPI _Success_(return != TIME_ZONE_ID_INVALID) DWORD WINAPI GetTimeZoneInformation(_Out_ LPTIME_ZONE_INFORMATION lpTimeZoneInformation); 

So the solution could be to replace this GetTimeZoneInformation () function with a custom function, which then returns an object that I can change on demand. But I don’t know how to do this, and I also don’t know if this works then or not, because it can only be a call to this function once when the application starts.

Does anyone know how to deal with this topic? I do not want to change any behavior, I only want to configure my own time zone, without violating the time calculation mechanism. It would be best if I could somehow mock these challenges without changing the production code for this. The fact is that this program is very large, old and ... changing as little as possible is always the best :)

If I did not find an existing post that solved my problem, then I am very sorry because I either did not find it or did not understand that this could help me.

+5
source share
1 answer

As you mentioned in your question, ridicule is a good way to solve this problem.

The quickest and easiest way I know this is to wrap all the methods you want to test in a new class that implements the Strategy strategy template:

It can switch between real implementation and mocks, as soon as you set the logic to the layout, it will return the value of the layout.

By default, it should be installed in the actual implementation.

A simple example with 'int Foo ()':

  Class Strategy{ int Foo() = o } Class StrategyImplementation { private: Strategy* logic; public: void SetLogic(Strategy* newLogic){logic=newLogic)} int Foo(){return logic->Foo()} } Class mock : public Strategy{ public: SetValue(int value){this.value = value} int Foo(){return value} private: int value; } Class Implementation : public Strategy{ int Foo(){ here_you_call_the_method_you_want_to_test } } 

All that remains is to change each call to GetTimeZoneInformation to a new Strategy. The easiest way to do this is with the "Singleton" design template.

It does not have a high risk, and the changes you will need to make is to change the calls from "Foo ()" to "StrategyImplementation :: GetInstance (). Foo ()"

0
source

Source: https://habr.com/ru/post/1259466/


All Articles