English 中文(简体)
Using gmock to mock objects copied in client code
原标题:

I could really use some help mocking the dependencies in this scenario.

I have a client function that I need to test. It takes a 3rd party API resource and calls a method on it, populating the rest of its arguments with the output:

void ClassA::enumerateConnectedDevices(ThirdPartyAPI::BusManager & busMgr, QList<QByteArray> &devices, QStringList &descriptions)

At some point in this function s flow, it makes a call on the busMgr object whose signature looks like this:

ThirdPartyAPI::Error busMgr.GetNumOfCameras(...)

And the client code that uses that call looks like this:

ThirdPartyAPI::Error err = busMgr.GetNumOfCameras(&numCameras);

if (err != ThirdPartyAPI::ERROR_OK) { // compares against some enum
    cout << "Error: " << err.GetDescription();
}

The problem I ran into is the copy of the error returned from GetNumberOfCameras to the err local variable. Mocking out the BusManager and Error classes works until that copy is made. Since the mock error is a subclass of ThirdPartyAPI::Error, the assignment operator slices the mock away. The third party API constructs valid error objects and returns them. Client code can only copy-construct or assign them.

All I need is to return valid mock error objects from my mocked BusManager for the SUT method to be testable. I m open to refactoring the SUT method as long as the intent remains the same.

Any suggestions will be greatly appreciated!

Cheers,

Alex

问题回答

It sounds like you re dealing with a classic problem in C++: the slicing problem when dealing with polymorphism. Because C++ doesn t know about your subclass at runtime, when you assign your mocked Error object to an Error reference, only the base class part gets copied (the "slicing").

One workaround for this is to use pointers or smart pointers instead of references. That way, polymorphism is preserved and the "whole" object is assigned, not just the base class part.

So, if it s possible, you can modify your client function to use pointers, and then use polymorphism as intended.

Here is an example of how this might look like:

void ClassA::enumerateConnectedDevices(ThirdPartyAPI::BusManager* busMgr, QList<QByteArray> &devices, QStringList &descriptions)
{
    int numCameras = 0;
    ThirdPartyAPI::Error* err = busMgr->GetNumOfCameras(&numCameras);

    if (*err != ThirdPartyAPI::ERROR_OK) { // compares against some enum
        cout << "Error: " << err->GetDescription();
    }

    // Don t forget to delete your pointers if not using smart pointers!
    delete err;
}

This way, your BusManager mock can return a pointer to a mock Error, and the full mock will be assigned, not just the base class part.

If the API doesn t allow you to use pointers, and you can t modify the API, this might be a bit more challenging. You would then need to find a way to either change the way you re testing, or refactor the code to be more testable. For example, you could wrap the API calls in a new layer of your own classes that does allow for pointers, and test that layer.

Let me know if this helps or if you need more assistance!





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签