Testing simple function with Gmock - test fails(should pass?) - googletest

I just started using Gtest/Gmocks and I'm struggling with an example. I have a simple class which has a member a function that returns a value(say 3). I'm trying to mock this test and check if the returned result is 3. For simplicity I wrote everything in a single file:
// Testing.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "gmock\gmock.h"
#include "gtest\gtest.h"
using ::testing::AtLeast;
using namespace std;
class MyClass{
public:
virtual int retValue() { return 3; }
virtual ~MyClass(){}
};
class FakeMyClass : public MyClass
{
public:
MOCK_METHOD0( retValue, int() );
};
TEST(TestForMyClass, TestRetVal)
{
FakeMyClass obj3;
EXPECT_EQ(obj3.retValue(), 3);
}
int _tmain(int argc, _TCHAR* argv[])
{
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
However my test fails and it says that expected result is 3 and my actual result is 0.
I've watched a couple of examples and I think I did everything as shown in there still the result is not what I'm expecting. Please help me see where I'm wrong and how can I make that test to pass. Thank you.

The simple answer to your question is:
You have an object of your mock FakeMyClass. This object will never return the value of the base class, if you override this method!!
If you want to test a simple method of a class, you do not need a mock. Just test your class:
class MyClass{
public:
virtual int retValue() { return 3; }
virtual ~MyClass(){}
};
TEST(TestForMyClass, TestRetVal)
{
MyClass obj3;
EXPECT_EQ(obj3.retValue(), 3);
}
Little bit more to mocking:
A principle of testing is to test in isolation. So, when your class is in a relation to another object, you have to mock this object. I suggest to take a look on an example
(e.g. klick).
In this example the Painter is the system under test. The Paintercommunicates with the Turtle, which is mocked.

Related

Initialising values inside structure

When I compiled the below code in gcc I get 1408.How does it work.Can we initialise values inside structure?
#include<iostream>
using namespace std;
struct Test {
int i=1408;
};
int main()
{
Test l;
int p=1508;
l.i==p;
cout<<l.i;
}
l.i==p; does nothing. == tests if two things are equal to each other. That's not an assignment.
You must have meant
l.i=p;
It is best to declare structure variable in struct body and intialise values using objects created using structure identifiers in your code identifier is Test.
Initialisation can be in the form of
Test t1 ={ 1408 };

Can I prevent nested deprecated method calls from raising warnings?

I have some library code which has some legacy code I'd like to move away from. To do so, I've started marking the outdated methods as deprecated. Where those methods call each other, I get deprecation warnings which I'd rather not see (the new functionality means you just need a single call as less of the internals of the classes workings are exposed).
Is there a way to suppress the deprecation warning for the call from OldMethod to OldMethodHelper? ..or a better way to do this altogether?
For example (in MyClass.h):
public ref class MyClass
{
public:
[Obsolete]
void OldMethodHelper();
[Obsolete]
void OldMethod();
void NewMethod();
};
In MyClass.cpp:
void MyClass::OldMethodHelper()
{
// Some old helper method that's called both from within this class and externally.
}
void MyClass::OldMethod()
{
OldMethodHelper(); // I don't want this call to raise a deprecation warning.
}
void MyClass::NewMethod()
{
// A new method which replaces the calls to both of the previous methods.
}
Code is called like this:
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
MyClass m;
m.OldMethodHelper(); // This should raise a deprecation warning.
m.OldMethod(); // This should raise a deprecation warning.
m.NewMethod();
return 0;
}
Edit - I found another post on SO which suggests using #pragma warning(disable: 4996) is a possibility but it seems like a bit of a clunky way to approach the problem to me:
void MyClass::OldMethod()
{
#pragma warning(push)
#pragma warning(disable: 4996) //4996 for _CRT_SECURE_NO_WARNINGS equivalent
OldMethodHelper(); // I don't want this call to raise a deprecation warning.
#pragma warning(pop)
}
Edit2 - Made some corrections / clarifications to the code example.
Speaking without proof, but maybe a macro could help here. Easier to show than to explain:
MyClass.h
---------
#ifndef MYCLASS_DEPRECATE
#define MYCLASS_DEPRECATE [Obsolete]
#endif
class MyClass
{
MYCLASS_DEPRECATE void OldMethodHelper();
...
}
MyClass.cpp
-----------
#define MYCLASS_DEPRECATE
#include "MyClass.h"
// The rest of the code

Qt5 emit a signal on behalf of another object

Is it possible to emit a signal on behalf of another QObject? the reason I would like to do this is that it would be very useful when writing mock/test code and simply want to simulate that a certain object emitted a signal.
I tried to use
QMetaObject::invokeMethod(otherObject, "mySignal", Q_ARG(QString, myArg));
because the documentation says:
[...] Invokes the member (a signal or a slot name) on the object obj.[...]
But this does not work for me. The signal is simply not emitted.
You can simply invoke the signal through the class like so:
otherObject.mySignal("Testing!");
Edit
Good point on the thread-safety issue. I got it to work as well with the invokeMethod solution by explicitly setting the connection type. If your objects are in different threads, you'd need to use the QueuedConnection rather than the DirectConnection. Here is my simple test case:
main.cpp
#include <QObject>
#include "Stub.h"
int main()
{
Stub stub;
Stub2 stub2;
QObject::connect(&stub, &Stub::TestSignal, &stub2, &Stub2::TestReceiver);
QMetaObject::invokeMethod(&stub,
"TestSignal",
Qt::DirectConnection,
Q_ARG(QString, "myArg"));
return 0;
}
Stub.h
#ifndef STUB_H
#define STUB_H
#include <QObject>
#include <QDebug>
class Stub : public QObject
{
Q_OBJECT
signals:
void TestSignal(QString s);
};
class Stub2 : public QObject
{
Q_OBJECT
public:
void TestReceiver(QString s) {qDebug() << "Got s:" << s;}
};
#endif // STUB_H

Capturing return type of unmanaged class using C++/CLI

I have a method in my native dll, that I want to use. The method returns an object of a type that is also in my native dll.I am trying to write a c++/CLI wrapper.
Now,
Can I get a return value as the object using C++/CLI and how do I do that?
Can we store and pass the native C++ object?
Should I need to create my own class resembling the native C++ class?
How would I marshal a class?
For Example,My native dll has these classes,
class X
{
/* some props and methods. */
};
Class Y
{
X* someMethod();
};
I need to wrap the someMethod class using C++/CLI. Will I be able to get the return value in the CLI?
Returning pointers to C++ objects from an exported function in a DLL is a pretty bad idea. It is a nasty memory management problem, you'd expect the client code to release the object. That can only come to a good end when both DLLs use the exact same version of the DLL version of the CRT (/MD compile option). If you can't recompile the native DLL then stop right now, you cannot make it work reliably or you'll have a big maintenance problem in the future.
Anyhoo, you need a wrapper for both classes. They should resemble this:
#pragma managed(push, off)
#include "xandy.h"
#pragma managed(pop)
using namespace System;
namespace something {
public ref class XWrapper {
X* mX;
public:
XWrapper(X* obj) : mX(obj) {}
~XWrapper() { this->!XWrapper(); }
!XWrapper() {
// Trouble is here!!!
delete mX;
}
};
public ref class YWrapper {
Y* mY;
public:
YWrapper() { mY = new Y; }
~YWrapper() { this->!YWrapper(); }
!YWrapper() { delete mY; }
XWrapper^ someMethod() {
return gcnew XWrapper(mY->someMethod());
}
};
}

SafeArrayPutElement method throws System.AccessViolationException

I am trying to pass an array of ints from C# to C++/CLI. Here's my code:
// SafeArrayTesting_PlusPlus.cpp
#include "stdafx.h"
#include <comdef.h>
using namespace System;
namespace SafeArrayTesting_PlusPlus
{
public ref class MyCppClass
{
public:
MyCppClass();
~MyCppClass();
void SetMyInts(array<int>^ myInts);
};
MyCppClass::MyCppClass(){}
MyCppClass::~MyCppClass(){}
void MyCppClass::SetMyInts(array<int>^ myInts)
{
// Create safearray
SAFEARRAY *safeArrayPointer;
SAFEARRAYBOUND arrayDim[1]; // one dimensional array
arrayDim[0].lLbound= 0;
arrayDim[0].cElements= myInts->Length;
safeArrayPointer = SafeArrayCreate(VT_UNKNOWN,1,arrayDim);
// copy ints to safearray
for (long lo= 0; lo < myInts->Length; lo++)
{
cli::pin_ptr<int> pinnedIntPointer = &(myInts[lo]);
SafeArrayPutElement(
safeArrayPointer,
&lo,
static_cast<void*> (pinnedIntPointer)); // line XX
}
// do something with the safearray here
}
}
// SafeArrayTesting_Main.cs
using SafeArrayTesting_PlusPlus;
namespace SafeArrayTesting_Main
{
class SafeArrayTesting_Main
{
static void Main()
{
var myCppClass = new MyCppClass();
myCppClass.SetMyInts(new[]{42});
}
}
}
When I run this, line XX throws the following exception:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I have a feeling that I'm doing something basically wrong with the SafeArrayPutElement method. Can you spot the error?
(By the way, I have asked a more complex variation of this question at Passing an array of interfaces from C# to C++/CLI . I think the difference is big enough to warrant two separate questions.)
safeArrayPointer = SafeArrayCreate(VT_UNKNOWN,1,arrayDim);
That creates an array of IUnknown interface pointers. SafeArrayPut() makes sure to increase the reference count of the interface pointer by calling IUnknown::AddRef() since it stores a copy of the pointer in the array.
You can see how that goes kaboom. Create an array of integers instead. Fix:
safeArrayPointer = SafeArrayCreate(VT_I4,1,arrayDim);