Return 在 google 模拟中引用 unique_ptr
Return reference to unique_ptr in google mock
我在 return 引用 google 模拟中的唯一指针时遇到问题。
我有一个对象 Foo,它有一个方法 operate(),我正试图在 google test/google 模拟框架中进行测试:
class Foo {
public:
Foo(BarInterface &bar) : bar{bar} {};
virtual ~Foo() = default;
void operate() { bar.getBaz()->startMeUp(); }
private:
BarInterface &bar;
};
测试 class 看起来像这样:
using ::testing::StrictMock;
using ::testing::ReturnRef;
class FooTest : public ::testing::Test {
protected:
StrictMock<BarMock> barMock;
std::unique_ptr<StrictMock<BazMock>> bazMock {new StrictMock<BazMock>()}; // This might be incorrect
Foo *foo;
virtual void SetUp() {
foo = new Foo(barMock);
}
virtual void TearDown() {
delete foo;
}
};
TEST_F(FooTest, BasicTest) {
EXPECT_CALL(barMock, getBaz()).WillOnce(ReturnRef(bazMock)); // Gives compilation error
EXPECT_CALL(*bazMock, startMeUp()); // Gives compilation error
foo->operate();
}
如您所见,我有两个被模拟的对象,Bar 和 Baz。
Baz mock 有一种方法,startMeUp():
class BazInterface {
public:
virtual ~BazInterface() = default;
virtual void startMeUp() = 0;
};
class BazMock : public BazInterface {
public:
MOCK_METHOD0(startMeUp, void());
};
Bar 方法 getBaz() returns 对唯一指针的引用根据:
class BarInterface {
public:
virtual ~BarInterface() = default;
virtual std::unique_ptr<BazInterface>& getBaz() = 0;
};
class BarMock : public BarInterface {
public:
MOCK_METHOD0(getBaz, std::unique_ptr<BazInterface>&());
};
问题是(至少)我无法正确使用两个 EXPECT_CALL()。我已经尝试 return 以多种方式使用 bazMock,但我总是遇到编译错误。
我还尝试通过 return 引用 shared_ptr 甚至普通指针来简化问题,但我也无法编译。
有人可以帮我解决问题吗?
编译输出如下:
gmock-actions.h: In instantiation of 'testing::internal::ReturnRefAction<T>::Impl<F>::Result testing::internal::ReturnRefAction<T>::Impl<F>::Perform(const ArgumentTuple&) [with F = std::unique_ptr<BazInterface>&(); T = std::unique_ptr<testing::StrictMock<BazMock> >; testing::internal::ReturnRefAction<T>::Impl<F>::Result = std::unique_ptr<BazInterface>&; testing::internal::ReturnRefAction<T>::Impl<F>::ArgumentTuple = std::tuple<>]':
foo_test.cc:30:1: required from here
gmock-actions.h:683:14: error: invalid initialization of reference of type 'testing::internal::ReturnRefAction<std::unique_ptr<testing::StrictMock<BazMock> > >::Impl<std::unique_ptr<BazInterface>&()>::Result {aka std::unique_ptr<BazInterface>&}' from expression of type 'std::unique_ptr<testing::StrictMock<BazMock> >'
return ref_;
最好的解决方法是将 BarInterface
的界面更改为 return 引用 BazInterface
而不是引用 unique_ptr
。无需向 BarInterface
用户公开指针。
如果您坚持要 return 引用 unique_ptr
,请这样做:
class BarMock : public BarInterface
{
public:
BarMock(std::unique_ptr<BazInterface> baz) {this->baz = std::move(baz);}
override std::unique_ptr<BazInterface>& getBaz(){
getBazWrapped();
return baz;
}
MOCK_METHOD0(getBazWrapped, void());
private:
std::unique_ptr<BazInterface> baz;
};
并在 FooTest
BazMock * bazMock { new <StrictMock<BazMock> };
BarMock barMock { std::unique_ptr<BazInterface>(bazMock)};
我在 return 引用 google 模拟中的唯一指针时遇到问题。 我有一个对象 Foo,它有一个方法 operate(),我正试图在 google test/google 模拟框架中进行测试:
class Foo {
public:
Foo(BarInterface &bar) : bar{bar} {};
virtual ~Foo() = default;
void operate() { bar.getBaz()->startMeUp(); }
private:
BarInterface &bar;
};
测试 class 看起来像这样:
using ::testing::StrictMock;
using ::testing::ReturnRef;
class FooTest : public ::testing::Test {
protected:
StrictMock<BarMock> barMock;
std::unique_ptr<StrictMock<BazMock>> bazMock {new StrictMock<BazMock>()}; // This might be incorrect
Foo *foo;
virtual void SetUp() {
foo = new Foo(barMock);
}
virtual void TearDown() {
delete foo;
}
};
TEST_F(FooTest, BasicTest) {
EXPECT_CALL(barMock, getBaz()).WillOnce(ReturnRef(bazMock)); // Gives compilation error
EXPECT_CALL(*bazMock, startMeUp()); // Gives compilation error
foo->operate();
}
如您所见,我有两个被模拟的对象,Bar 和 Baz。 Baz mock 有一种方法,startMeUp():
class BazInterface {
public:
virtual ~BazInterface() = default;
virtual void startMeUp() = 0;
};
class BazMock : public BazInterface {
public:
MOCK_METHOD0(startMeUp, void());
};
Bar 方法 getBaz() returns 对唯一指针的引用根据:
class BarInterface {
public:
virtual ~BarInterface() = default;
virtual std::unique_ptr<BazInterface>& getBaz() = 0;
};
class BarMock : public BarInterface {
public:
MOCK_METHOD0(getBaz, std::unique_ptr<BazInterface>&());
};
问题是(至少)我无法正确使用两个 EXPECT_CALL()。我已经尝试 return 以多种方式使用 bazMock,但我总是遇到编译错误。 我还尝试通过 return 引用 shared_ptr 甚至普通指针来简化问题,但我也无法编译。 有人可以帮我解决问题吗?
编译输出如下:
gmock-actions.h: In instantiation of 'testing::internal::ReturnRefAction<T>::Impl<F>::Result testing::internal::ReturnRefAction<T>::Impl<F>::Perform(const ArgumentTuple&) [with F = std::unique_ptr<BazInterface>&(); T = std::unique_ptr<testing::StrictMock<BazMock> >; testing::internal::ReturnRefAction<T>::Impl<F>::Result = std::unique_ptr<BazInterface>&; testing::internal::ReturnRefAction<T>::Impl<F>::ArgumentTuple = std::tuple<>]':
foo_test.cc:30:1: required from here
gmock-actions.h:683:14: error: invalid initialization of reference of type 'testing::internal::ReturnRefAction<std::unique_ptr<testing::StrictMock<BazMock> > >::Impl<std::unique_ptr<BazInterface>&()>::Result {aka std::unique_ptr<BazInterface>&}' from expression of type 'std::unique_ptr<testing::StrictMock<BazMock> >'
return ref_;
最好的解决方法是将 BarInterface
的界面更改为 return 引用 BazInterface
而不是引用 unique_ptr
。无需向 BarInterface
用户公开指针。
如果您坚持要 return 引用 unique_ptr
,请这样做:
class BarMock : public BarInterface
{
public:
BarMock(std::unique_ptr<BazInterface> baz) {this->baz = std::move(baz);}
override std::unique_ptr<BazInterface>& getBaz(){
getBazWrapped();
return baz;
}
MOCK_METHOD0(getBazWrapped, void());
private:
std::unique_ptr<BazInterface> baz;
};
并在 FooTest
BazMock * bazMock { new <StrictMock<BazMock> };
BarMock barMock { std::unique_ptr<BazInterface>(bazMock)};