'Injecting a Mock using registerConstructor
I've successfully set up a Mock for injection via fruit using .replace(get*Component).with(getMock*Component)
like so:
#include <gmock/gmock.h>
#include "mockBar.h"
#include "classUnderTest.h"
namespace foo {
fruit::Component<ClassUnderTest, MockBar> getMainComponent()
{
return fruit::createComponent()
.replace(getBarComponent)
.with(getMockBarComponent)
.install(getClassUnderTestComponent);
}
class ClassUnderTestTest : public ::Testing::Test
{
protected:
fruit::Injector<ClassUnderTest, MockBar> injector{getMainComponent};
MockBar *mockBar{injector};
ClassUnderTest *classUnderTest{injector};
};
// TEST_F declarations
}
For the base mock as I'm writing that myself I have no problems because I can utilise the INJECT()
macro on the default constructor and bind the mock to the interface in getMock*Component
like so:
#include "bar.h"
namespace foo
{
class MockBar : public Bar
{
public:
INJECT(MockBar()) = default;
// MOCK_METHOD declarations
};
fruit::Component<Bar> getMockBarComponent()
{
return fruit::createComponent()
.bind<Bar, MockBar>();
}
}
For a StrictMock
where the constructor is defined in the googlemock library as I understand things I should be using .registerConstructor<StrictMock<Mock*>()>()
instead and this is where my problems start.
No explicit binding nor C::Inject definition was found for T.
For the sake of troubleshooting I've removed the INJECT()
macro and applied this logic to the base mock like so which gives me the same error when trying to build:
#include "bar.h"
namespace foo
{
class MockBar : public Bar
{
public:
// MOCK_METHOD declarations
};
fruit::Component<Bar> getMockBarComponent()
{
return fruit::createComponent()
.registerConstructor<MockBar()>()
.bind<Bar, MockBar>();
}
}
Tried with registerProvider()
like so for the sake of argument but got the same error:
fruit::Component<Bar> getMockBarComponent()
{
return fruit::createComponent()
.registerProvider([](){ return new MockBar; })
.bind<Bar, MockBar>();
}
registerFactory<Mock*()>()
would appear to be excessive for what I'm trying to do (I only need the one instance) so I've not tried delving into that.
My presumption because I'm new to both googlemock and fruit and because my C++ experience isn't stellar is there's a nuance I'm missing here regardless of what I've read to the contrary.
Any ideas/suggestions for what I should try next or blaringly obvious errors I've made?
I can abstract out some more of my code if I haven't captured everything of importance here.
Ta in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|