Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking a class with parameters #72

Closed
zichaelpath opened this issue May 1, 2019 · 9 comments
Closed

Mocking a class with parameters #72

zichaelpath opened this issue May 1, 2019 · 9 comments

Comments

@zichaelpath
Copy link

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

@mat007
Copy link
Owner

mat007 commented May 1, 2019

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?

@zichaelpath
Copy link
Author

zichaelpath commented May 1, 2019

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

@mat007
Copy link
Owner

mat007 commented May 1, 2019

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:

@zichaelpath
Copy link
Author

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

@mat007
Copy link
Owner

mat007 commented May 2, 2019

Something like

MOCK_CLASS(MockClass, BaseClass)
{
    MOCK_STATIC_METHOD(MockClassConstructor, 2, (ParameterClass1&, ParameterClass2*))

    MockClass(ParameterClass1&, p1 ParameterClass2* p2)
        : BaseClass(p1, p2)
    {
        MockClassConstructor(p1, p2);
    }
};

?

@zichaelpath
Copy link
Author

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

@mat007
Copy link
Owner

mat007 commented May 2, 2019

Can you show me the code which fails?

@zichaelpath
Copy link
Author

zichaelpath commented May 2, 2019

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

@mat007
Copy link
Owner

mat007 commented May 2, 2019

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

@mat007 mat007 closed this as completed Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants