Mocking a class with parameters
Hello, I am currently trying to test out mocking a class that has parameters in its constructor, how would i implement that using MOCK_BASE_CLASS? In Visual Studio, there are no red underlines, however when I Compile the program, i get the following error: no appropriate default constructor available I am using visual studio 2013 and am developing on Windows Embedded Compact 2013
Hi,
The documentation has a few examples: http://turtle.sourceforge.net/turtle/reference.html#turtle.reference.creation.constructor Does this answer your question? If not can you provide the code you're trying to compile?
The documentation helps, however the compiler is still giving me a hard time.
I'm mocking a class that takes two classes as parameters I am using the MOCK_CONSTRUCTOR macro to mock my constructor and I am passing it like this:
"MOCK_CONSTRUCTOR(MockClass, 2, (ParameterClass1&, ParameterClass2*));" Am I using the wrong class in the first parameter of MOCK_CONSTRUCTOR? i end up getting the following as errors:
- syntax error )
- "BaseClass": no appropriate constructor available
- no default constructor exists for BaseClass
I know that there is an existing constructor for BaseClass, but intellisense seems to think there isnt
You need an «identifier» (see the documentation section I linked):
MOCK_CONSTRUCTOR(MockClass, 2, (ParameterClass1&, ParameterClass2*), MockClassConstructor)
To understand how to use an «identifier» later on you can read:
- http://turtle.sourceforge.net/turtle/reference.html#turtle.reference.creation
- http://turtle.sourceforge.net/turtle/reference.html#turtle.reference.expectation
That code really helped me out and I can't believe I didn't see that in my first pass through of the documentation. Is there a way to mock a class that does not have a default constructor? in the software I am testing, none of our classes have default constructors
Something like
MOCK_CLASS(MockClass, BaseClass)
{
MOCK_STATIC_METHOD(MockClassConstructor, 2, (ParameterClass1&, ParameterClass2*))
MockClass(ParameterClass1&, p1 ParameterClass2* p2)
: BaseClass(p1, p2)
{
MockClassConstructor(p1, p2);
}
};
?
That's helped tremendously. last question i have for you is what exactly constitutes an identifier? when i run the boost test where I am testing my mock class, it keeps saying I have an unknown identifier. I've looked in the code, but can't seem to find anything
Can you show me the code which fails?
MOCK_CONSTRUCTOR(MockClassName, 2, BaseClass(ClassParam1&, ClassParam2*), MockClassName); I was able to get mock constructor working by adding the class name to the third parameter (something i forgot to do before). The following code compiles without error, but when I run the unit test that is using this mock class, i get the following error: unknown location(0): error in "testName": unexpected call: MockClassName ( ? , 0050CF80) I'm pretty sure the last number is a memory address and I am using this class is a pointer. I would like to note that using the regular class with the same test suite works perfectly fine
You need to set an expectation to define how you expect the constructor to be called, see http://turtle.sourceforge.net/turtle/reference.html#turtle.reference.expectation