gpt4 book ai didi

c++ - 在谷歌模拟中返回对 unique_ptr 的引用

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:01 26 4
gpt4 key购买 nike

我在返回对 google mock 中唯一指针的引用时遇到问题。我有一个对象 Foo,它有一个方法 operate(),我正在尝试在 google test/google mock 框架中测试它:

class Foo {
public:
Foo(BarInterface &bar) : bar{bar} {};
virtual ~Foo() = default;
void operate() { bar.getBaz()->startMeUp(); }
private:
BarInterface &bar;
};

测试类如下所示:

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() 根据以下条件返回对唯一指针的引用:

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() 正确。我尝试过以多种方式返回 bazMock,但总是出现编译错误。我还尝试通过返回对 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 的接口(interface)以返回对 BazInterface 的引用,而不是引用 unique_ptr。无需向 BarInterface 用户公开指针。
如果您坚持要返回对 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)};

关于c++ - 在谷歌模拟中返回对 unique_ptr 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48058879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com