为什么 GMOCK 对象不是由 EXPECT_CALL 在依赖注入中设置的 return 值
Why does GMOCK object not return values set by EXPECT_CALL in dependency injection
我有以下要模拟的对象:
class Esc {
public:
Esc() = default;
virtual ~Esc() {}
virtual int GetMaxPulseDurationInMicroSeconds() const noexcept{
return 100;
}
};
我写了这个模拟:
class MockEsc : public Esc {
public:
MockEsc(){}
MockEsc(const MockEsc&){}
MOCK_METHOD(int, GetMaxPulseDurationInMicroSeconds, (), (const, noexcept, override));
};
这是被测单元调用上述Esc.GetMaxPulseDurationInMicroSeconds()
方法
class LeTodar2204{
public:
LeTodar2204() = delete;
explicit LeTodar2204(std::unique_ptr<Esc> esc) : esc_(std::move(esc)){}
int CallingMethod(){
int a = esc_->GetMaxPulseDurationInMicroSeconds();
return a;
}
private:
std::unique_ptr<Esc> esc_;
在我的测试夹具的设置中,我想将我的模拟设置为 return a 1 并将其注入到被测单元中。
class Letodar2204Tests : public ::testing::Test {
protected:
Letodar2204Tests() {}
virtual void SetUp() {
EXPECT_CALL(esc_, GetMaxPulseDurationInMicroSeconds()).WillOnce(::testing::Return(1));
unit_under_test_ = std::make_unique<propulsion::LeTodar2204>(std::make_unique<MockEsc>(esc_));
}
MockEsc esc_;
std::unique_ptr<propulsion::LeTodar2204> unit_under_test_;
};
现在在测试中我调用了应该调用模拟方法的方法,但是我的模拟对象的 GetMaxPulseDurationInMicroSeconds
只有 returns 0(又名默认值)并警告我无趣函数调用。
这是测试
TEST_F(Letodar2204Tests, get_pulse_duration) {
EXPECT_CALL(esc_, GetMaxPulseDurationInMicroSeconds()).WillOnce(::testing::Return(1));
auto duration = unit_under_test_->CallingMethod();
ASSERT_EQ(duration, 1);
}
我错过了什么?
因为您实际上是在将期望值分配给您无论如何都要复制的对象。 esc_
默认构造函数将在实例化 Letodar2204Tests
后调用。现在,在 SetUp
中,您在 class 字段 esc_
上分配了一个期望值,然后,基于 esc_
创建一个 全新的 对象(在堆上,使用 make_unique
)使用它的 copy-ctor。期望也不会被神奇地复制。我相信您应该将 unique_ptr<MockEsc> esc_
存储为 class' 字段,在 SetUp
内的堆上实例化它并注入到 LeTodar
:
class Letodar2204Tests : public ::testing::Test {
protected:
Letodar2204Tests() {}
virtual void SetUp() {
esc_ = std::make_unique<MockEsc>();
EXPECT_CALL(*esc_, GetMaxPulseDurationInMicroSeconds()).WillOnce(::testing::Return(1));
unit_under_test_ = std::make_unique<propulsion::LeTodar2204>(esc_);
}
std::unique_ptr<MockEsc> esc_;
std::unique_ptr<propulsion::LeTodar2204> unit_under_test_;
};
在这里,您隐式调用 copy-ctor:std::make_unique<MockEsc>(esc_)
您可以做一个简单的测试:在 MockEsc 中标记 copy ctor 'deleted',您会看到您的项目不再编译(至少不应该是 :D)
我有以下要模拟的对象:
class Esc {
public:
Esc() = default;
virtual ~Esc() {}
virtual int GetMaxPulseDurationInMicroSeconds() const noexcept{
return 100;
}
};
我写了这个模拟:
class MockEsc : public Esc {
public:
MockEsc(){}
MockEsc(const MockEsc&){}
MOCK_METHOD(int, GetMaxPulseDurationInMicroSeconds, (), (const, noexcept, override));
};
这是被测单元调用上述Esc.GetMaxPulseDurationInMicroSeconds()
方法
class LeTodar2204{
public:
LeTodar2204() = delete;
explicit LeTodar2204(std::unique_ptr<Esc> esc) : esc_(std::move(esc)){}
int CallingMethod(){
int a = esc_->GetMaxPulseDurationInMicroSeconds();
return a;
}
private:
std::unique_ptr<Esc> esc_;
在我的测试夹具的设置中,我想将我的模拟设置为 return a 1 并将其注入到被测单元中。
class Letodar2204Tests : public ::testing::Test {
protected:
Letodar2204Tests() {}
virtual void SetUp() {
EXPECT_CALL(esc_, GetMaxPulseDurationInMicroSeconds()).WillOnce(::testing::Return(1));
unit_under_test_ = std::make_unique<propulsion::LeTodar2204>(std::make_unique<MockEsc>(esc_));
}
MockEsc esc_;
std::unique_ptr<propulsion::LeTodar2204> unit_under_test_;
};
现在在测试中我调用了应该调用模拟方法的方法,但是我的模拟对象的 GetMaxPulseDurationInMicroSeconds
只有 returns 0(又名默认值)并警告我无趣函数调用。
这是测试
TEST_F(Letodar2204Tests, get_pulse_duration) {
EXPECT_CALL(esc_, GetMaxPulseDurationInMicroSeconds()).WillOnce(::testing::Return(1));
auto duration = unit_under_test_->CallingMethod();
ASSERT_EQ(duration, 1);
}
我错过了什么?
因为您实际上是在将期望值分配给您无论如何都要复制的对象。 esc_
默认构造函数将在实例化 Letodar2204Tests
后调用。现在,在 SetUp
中,您在 class 字段 esc_
上分配了一个期望值,然后,基于 esc_
创建一个 全新的 对象(在堆上,使用 make_unique
)使用它的 copy-ctor。期望也不会被神奇地复制。我相信您应该将 unique_ptr<MockEsc> esc_
存储为 class' 字段,在 SetUp
内的堆上实例化它并注入到 LeTodar
:
class Letodar2204Tests : public ::testing::Test {
protected:
Letodar2204Tests() {}
virtual void SetUp() {
esc_ = std::make_unique<MockEsc>();
EXPECT_CALL(*esc_, GetMaxPulseDurationInMicroSeconds()).WillOnce(::testing::Return(1));
unit_under_test_ = std::make_unique<propulsion::LeTodar2204>(esc_);
}
std::unique_ptr<MockEsc> esc_;
std::unique_ptr<propulsion::LeTodar2204> unit_under_test_;
};
在这里,您隐式调用 copy-ctor:std::make_unique<MockEsc>(esc_)
您可以做一个简单的测试:在 MockEsc 中标记 copy ctor 'deleted',您会看到您的项目不再编译(至少不应该是 :D)