How to mock a local variable initialized with the member variable - googletest

I have two classes Class A and Class SRD (Sample classes for understanding the problem. Real classes are different). Both classes have same Function(method1) with same arguments. Both are not derived from different Classes.
Class SRD is the member of Class A. a function in Class A creates a new object for SRD and calls method1(). It should call the mock function. but it calls the actual implementation
I have Written mock classes for both the classes and defined the mock method in both the classes and Wrote EXPECT_CALL in TEST function
class A{
private:
SRD* srd;
public :
bool Method1();
bool MethodX();
SRD* getsrd() {return srd;}
};
bool A :: MethodX()
{
srd.Method1(); // Not Calling Mock Method - Calling Actual
//Implementation
}
bool A::Method1()
{
....
}
class SRD{
public:
bool Method1();
};
class MockSRD : public SRD{
MOCK_METHOD0(Method1, bool())
};
class MockA : public MockA{
MOCK_METHOD0(Method1, bool())
};
bool SRD::Method1()
{
....
}
class TestA : public A {};
TEST_F(TestA, test1)
{
MockSRD srd;
EXPECT_CALL(srd, Method1())
.Times(testing::AnyNumber())
.WillRepeatedly(Return(true));
srd.Method1() //Working fine - Caling mock Method;
MethodX()
}
When i call s1.Method1(), It should call the mock method. how should i do that ?
I don't want to change the production code.

Thanks for taking time to respond the Question . #Chris Oslen & #sklott
I forgot to make the base class method to Virtual. Its worked fine when i change the base class methods

Related

Why need to extends Object?

I saw simple class which was look like:
class SomeClass extends Object{
int a;
int b;
...
...
}
Why this class was extended an Object class? As in documentation was written "Because Object is the root of the Dart class hierarchy, every other Dart class is a subclass of Object." in https://api.dartlang.org/stable/2.4.0/dart-core/Object-class.html.
What will happened if we will not extends Object? Or maybe it will be useful in some specific problems?
All dart classes implicitly extend Object, even if not specified.
This can be verified using the following code:
class Foo {}
void main() {
var foo = Foo();
print(foo is Object); // true
}
Even null implements Object, which allows doing:
null.toString()
null.hashCode
null == something

TypeScript function signature in abstract class doesn't need to match interface

In the following example, interface IFoo declares a function signature requiring two number arguments. Abstract class BaseFoo implements this interface, but declares the function with a different signature. Finally, concrete class Foo extends BaseFoo and implements BaseFoo's version of the function declaration.
interface IFoo {
func(x: number ): number
}
abstract class BaseFoo implements IFoo {
abstract func(x: number): number
}
class Foo extends BaseFoo {
func() { return -1 } // Does not match interface func declaration
}
let foo: IFoo = new Foo() // Should not be able to instantiate a Foo as an IFoo
let y = foo.func() // Should not be able to call without an argument
console.log(y)
This contrived example illustrates something that happened in real life: I had an existing interface in a codebase. I updated one of it's function's signatures, with the expectation that the compiler would help me find all the classes who would need to be updated. But, no errors.
Why am I allowed to instantiate an abstract class with a function signature that doesn't match the interface?

OOP - How to create an interface in Reason

Let's say I have the following abstractProductA class with a public method called methodA :
class abstractProductA = {
pub methodA => "name";
};
I would like to create an interface that says function methodA should always return a string. Something similar to
interface abstractProductA {
abstractProductA(): string
}
only in reason, and then have class implement it. Any suggestions are more than welcome. Thank you
What you're really asking for it seems is how to define and use an abstract class, which is called a virtual class in OCaml/Reason:
class virtual virtualProductA = {
pub virtual methodA: string;
};
class abstractProductA = {
inherit virtualProductA;
pub methodA = "name";
};
An interface is more for consumers to abstract away an implementation, and while a virtual class can be used as an interface by itself, since OCaml/Reason objects are structurally typed you can also just specify the object type you need. And of course you can bind it to a name if you like:
type interfaceA = {.
methodA : string
};
let f (p: interfaceA) => Js.log p#methodA;
f (new abstractProductA);

How to call constructor in php?

If I create a class and define a public constructor of it and I also create a child class of the parent class, it also have constructor.
Then how can I call these two constructors from one of the method of subclass? I mean how to call two or more constructor from one method of a child class in php?
In C++:
You can call just by creating a child object.
When you just create a child object it first calls Parent Constructor and then The child Constructor.
Example:
Class Parent {
void Parent :: Parent() {
cout << "I am parent Constructor!" << endl;
}
};
Class Child : Public Parent() {
void Child :: Child() {
cout << "I am Child Constructor" << endl;
}
};
int main() {
Child childobj;
}
Output:
"I am parent Constructor!"
"I am Child Constructor"
For PHP
class Parent {
public function __construct($bypass = false) {
// Only perform actions inside if not bypassing.
if (!$bypass) {
}
}
}
class Child extends Parent {
public function __construct() {
$bypassPapa = true;
parent::__construct($bypassPapa);
}
}
I am answering this based specifically on C++ programming, as I am not certain which OOP language you are using, but I expect that the principles, if not the specific syntax, will apply.
When you define a class with at least one constructor, the compiler will not generate an implicit constructor. As such, if the constructor(s) you define for the base class require parameters, they must be included in a specific call from the constructor in the child class since there will be no parameter free constructor to call.
class Parent
{
public:
Parent(int a,int a)
:a(a),
b(b)
{
cout<<"Parent constructor "<<a<<b;
}
~Parent()
{}
private:
int a;
int b;
};
class Child : public Parent
{
public:
Child()
:c(5) //error: implicit constructor for Parent is not found
{
cout<<"Child constructor "<<c;
}
~Child()
{}
private:
int c;
};
int main()
{
Child x;
return 0;
}
This problem can be corrected by including a call to the Parent constructor within the constructor for the Child class as follows:
.
.
.
Child()
:Parent(3,4), // Explicit call to Parent constructor
c(5)
{
cout<<"Child constructor "<<c;
}
.
.
.
Hope this helps.
In C#;
When you create an instance of subclass, if the base class has a parameter-less constructor, It will be called. But if the base class has parameter constructor, you may call the constructor of that by following syntax.
Class SubClass : BaseClass(...)
{
...
}
In order to call constructor in other methods you need to have a protected method which was called by constructor, then you can call it from another method. Please note that you cannot call constructor from another method because it's a mechanism for instantiating (It should be called when an instance of that type is created)

OO - Reduce boilerplate/forwarding code

Imagine the following: I have a bunch of DTO's that inherit from Foo class
class Foo { }
class FooA : Foo { }
class FooB : Foo { }
class FooX : Foo { }
Than I have one class that have encapsulated all the related logic and orchestration related with Foo data types. I provide a method DoSomethingWithData(Foo data) that do all the logic related to data provided by argument
The method implementation is something like this:
void DoSomething(Foo data)
{
if (data is FooA)
DoSomethingWithFooA((FooA) data);
if (data is FooB)
DoSomethingWithFooB((FooA)data);
if (data is FooX)
DoSomethingWithFooC((FooA)data);
}
This is a very simplified example. The advantage of this approach is:
The "Client" invoke always the DoSomething method independently of
the Foo data type
If I add a new type I only have to change the method DoSomething
What i dont like is the downcasting
The alternative is instead of exposing only DoSomething method I expose a method by each Foo data type. The advantage is that we dont have downcast but increases the boilerplate/forwarding code.
What do you prefer? Or do you have other approaches?
In this case, I would approach the problem like this (I will use Java for this example).
In your approach, for every subclass of Foo you have to provide a specific processing logic - as you have shown, and cast the Foo object to its sub-type. Moreover, for every new class that you add, you have to change the DoSomething(Foo f) method.
You can make the Foo class an interface:
public interface Foo{
public void doSomething();
}
Then have your classes implement this interface:
public class FooA iplements Foo {
public void doSomething(){
//Whatever FooA needs to do.
}
}
public class FooB implements Foo {
public void doSomething(){
//Whatever FooB needs to do.
}
}
And so on. Then, the client can call the doSomething() method:
...
Foo fooA = new FooA();
Foo fooB = new FooB();
fooA.doSomething();
fooB.doSomething();
...
This way, you don't have to cast the object at run-time and if you add more classes, you don't have to change your existing code, except the client that has to call the method of a newly added object.