chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes

I'm doing the following in a test:

WHEN_CALLED(cpMac->CreateDispatch(_)).Return(1);

WHEN_CALLED(cpMac->Open(_, _)).Throw(&openFailure);

First one works, second one calls the real code onstead of throwing an exception.

Is there only one WHEN_CALLED allowed?

asked by ed123 (600 points)

1 Answer

0 votes

Hi Ed,

No, there is not only one WHEN_CALLED allowed.

The issue might be something else.

I can't tell you exactly what is the issue, I don't have enough information about your code and test.

If you want to proceed please elaborate more.

Cheers,

Alon Sapozhnikov.

Support Specialist. 

answered by Alon_TypeMock (8.9k points)
OK, that's great to know. What information would you need to further investigate this issue?

Thanks,

Ed

Hi Ed,

For starters i will need the class you are faking (including the method open which you are changing it's behavior) and the test itself.

Cheers,

Alon Sapozhnikov.

Support Specialist.

The class I\'m faking:

// Machine generated IDispatch wrapper class(es) created with Add Class from Typelib Wizard

// CPmacDevice wrapper class

class CPmacDevice : public COleDispatchDriver

{

public:

CPmacDevice(){} // Calls COleDispatchDriver default constructor

CPmacDevice(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}

CPmacDevice(const CPmacDevice& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}

// Attributes

public:

// Operations

public:

// IPmacDevice methods

public:

void Open(long dwDevice, BOOL * pbSuccess)

{

static BYTE parms[] = VTS_I4 VTS_PBOOL ;

InvokeHelper(0x1, DISPATCH_METHOD, VT_EMPTY, NULL, parms, dwDevice, pbSuccess);

}

void Close(long dwDevice)

{

static BYTE parms[] = VTS_I4 ;

InvokeHelper(0x2, DISPATCH_METHOD, VT_EMPTY, NULL, parms, dwDevice);

}

void DataStart(long dwDevice, long resolution, long period)

{

static BYTE parms[] = VTS_I4 VTS_I4 VTS_I4 ;

InvokeHelper(0x5, DISPATCH_METHOD, VT_EMPTY, NULL, parms, dwDevice, resolution, period);

}

void DataStop(long dwDevice)

{

static BYTE parms[] = VTS_I4 ;

InvokeHelper(0x6, DISPATCH_METHOD, VT_EMPTY, NULL, parms, dwDevice);

}

void DataCollect(long dwDevice, VARIANT * pvArray, BOOL * pbSuccess)

{

static BYTE parms[] = VTS_I4 VTS_PVARIANT VTS_PBOOL ;

InvokeHelper(0x7, DISPATCH_METHOD, VT_EMPTY, NULL, parms, dwDevice, pvArray, pbSuccess);

}

void DPRBackGroundVar(long dwDevice, BOOL bOn)

{

static BYTE parms[] = VTS_I4 VTS_BOOL ;

InvokeHelper(0x8, DISPATCH_METHOD, VT_EMPTY, NULL, parms, dwDevice, bOn);

}

/ IPmacDevice properties

public:

};

The test using MSTEST with VS2019:

#include \"pch.h\"

#include \"CppUnitTest.h\"

#include \"Isolator.h\"

#include \"XrmMotionHandler.h\"

#include \"XrmMotionController.h\"

#include \"XrmTechnosoftMotionController.h\"

#include \"CPmacDevice.h\"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace XrmMotionUnitTests

{

TEST_CLASS(XrmDeltaTauMotionControllerUnitTests_MockCPmacDeviceTests)

{

public:

TEST_CLASS_INITIALIZE(Setup)

{

static bool bTestCreatedKey = false;
HKEY hKey;

string strKey = \"System\\\\CurrentControlSet\\\\Services\\\\Pmac\\\\pmaceth0\";

string strIPAddressName = \"IPAddress\";

string strIPAddress = \"172.24.68.50\";

DWORD dwType = REG_DWORD;

DWORD dwSize, dwValue;

string strKeyDevice = \"System\\\\CurrentControlSet\\\\Services\\\\Pmac\\\\Device0\";

string strLocation = \"Location\";

DWORD nDataLength = 4;

DWORD nLocationValue = 3; // magic number of some sort

string strEnumeration = \"Enumeration\";

DWORD nEnumerationValue = 0;

HKEY hKey2;

// set up the pmac registry information so that we can

// proceed with the checks before starting the test

if (RegOpenKeyA(HKEY_LOCAL_MACHINE, strKey.c_str(), &hKey) != ERROR_SUCCESS)

{

bTestCreatedKey = true;

if (RegCreateKeyExA(HKEY_LOCAL_MACHINE, strKey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL) != ERROR_SUCCESS)

{

Assert::Fail(L\"Failed to create pmaceth0 key in registry\");

}

}

dwSize = 4;

dwValue = htonl(843323564);

if (RegQueryValueEx(hKey, L\"IPAddress\", nullptr, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)

{

if (RegSetKeyValueA(hKey, NULL, strIPAddressName.c_str(), REG_DWORD, (LPBYTE)&dwValue, dwSize) != ERROR_SUCCESS)

{

Assert::Fail(L\"Failed to create IPAddress key in registry\");

}

}

if (RegOpenKeyA(HKEY_LOCAL_MACHINE, strKeyDevice.c_str(), &hKey2) != ERROR_SUCCESS)

{

if (RegCreateKeyExA(HKEY_LOCAL_MACHINE, strKeyDevice.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey2, NULL) != ERROR_SUCCESS)

{

Assert::Fail(L\"Failed to create Device0 key in registry\");

}

}

if (RegQueryValueEx(hKey, L\"Location\", nullptr, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)

{

if (RegSetKeyValueA(hKey2, NULL, strLocation.c_str(), REG_DWORD, &nLocationValue, nDataLength) != ERROR_SUCCESS)

{

Assert::Fail(L\"Failed to create Location key in registry\");

}

}

if (RegQueryValueEx(hKey, L\"Enumeration\", nullptr, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)

{

if (RegSetKeyValueA(hKey2, NULL, strEnumeration.c_str(), REG_DWORD, &nEnumerationValue, nDataLength) != ERROR_SUCCESS)

{

Assert::Fail(L\"Failed to create Enumeration key in registry\");

}

}

}

TEST_METHOD(OpenEthernetDeviceWithExceptionThrown)

{

// As a general recommendation, we should always use the

// AAA unit test pattern (Arrange - Act - Assert)

XrmDeltaTauMotionController* pDeltaTau = nullptr;

//**********************************************************

// Arrange (create and/or initialize the test variables specifically for this test case)

//**********************************************************

exception openFailure(\"Open threw an exception\");

CPmacDevice* cpMac = FAKE_ALL<CPmacDevice>();

XrmMotionHandler* motionHandler = XrmMotionHandler::GetTheMotionHandler(true);

int controllerCount = motionHandler->GetTotalMotionControllersDefined();

for (int i = 0; i < controllerCount; i++)

{

if (motionHandler->GetMotionController(i)->GetControllerType() == XrmMotionTypes::DELTA_TAU)

{

pDeltaTau = static_cast<XrmDeltaTauMotionController*>(motionHandler->GetMotionController(i));

WHEN_CALLED(cpMac->CreateDispatch(_)).Return(1);//Throw(&openFailure);

WHEN_CALLED(cpMac->Open(_, _)).Throw(&openFailure);

pDeltaTau->SetCPmacDevice(cpMac);

break;

}

}

//**********************************************************

// Act (execute the test actions)

//**********************************************************

pDeltaTau->Initialize();

//**********************************************************

// Assert (test state/results to ensure outcome is the expected result)

//**********************************************************

}

TEST_CLASS_CLEANUP(Teardown)

{

ISOLATOR_CLEANUP();

}

};

}
Just wondering if there's any more information I need to give you. If not, any thoughts?

Hi Ed,

Unfortunately, it's difficult to reproduce the issue like that.

If you can please open a ticket in our Premium Support with all the relevant information, if you can also create a sample project that reproduces the issue it will be great.

Cheers,

Alon Sapozhnikov.

Support Specialist.

...