如何模拟构造函数具有std :: string参数的类对象

时间:2018-09-16 07:17:17

标签: c++ googletest gmock

我有一个模拟基类对象的问题,它的构造函数有一个参数std :: string。为什么编译器会收到此错误:没有匹配的函数可调用âBaseMock:: BaseMock(const std :: __ cxx11 :: basic_string&) 谢谢。

env:
g++ (Ubuntu 7.3.0-16ubuntu3) 7.3.0
Gtest 1.9.0

错误输出:

In file included from /usr/local/include/gmock/gmock.h:64:0,
                 from UT_Derived.cpp:2:
/usr/local/include/gmock/gmock-generated-nice-strict.h: In instantiation of âtesting::StrictMock<M>::StrictMock(A&&) [with A = const std::__cxx11::basic_string<char>&; MockClass = BaseMock]â:
/usr/include/c++/7/bits/unique_ptr.h:825:30:   required from âtypename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = testing::StrictMock<BaseMock>; _Args = {const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}; typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<testing::StrictMock<BaseMock> >]â
**UT_Derived.cpp:23:62:   required from here
/usr/local/include/gmock/gmock-generated-nice-strict.h:328:64: error: no matching function for call to âBaseMock::BaseMock(const std::__cxx11::basic_string<char>&)â**
   explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
                                                                ^
In file included from UT_Derived.cpp:4:0:
BaseMock.hpp:6:7: note: candidate: BaseMock::BaseMock() <deleted>
 class BaseMock : public Base
       ^~~~~~~~
BaseMock.hpp:6:7: note:   candidate expects 0 arguments, 1 provided
BaseMock.hpp:6:7: note: candidate: BaseMock::BaseMock(const BaseMock&) <deleted>
BaseMock.hpp:6:7: note:   no known conversion for argument 1 from âconst std::__cxx11::basic_string<char>â to âconst BaseMock&â
BaseMock.hpp:6:7: note: candidate: BaseMock::BaseMock(BaseMock&&) <deleted>
BaseMock.hpp:6:7: note:   no known conversion for argument 1 from âconst std::__cxx11::basic_string<char>â to âBaseMock&&â

代码:

#ifndef BASE_H_
#define BASE_H_

#include <string>

class Base
{
    public:
        Base(std::string str);
        void ShowString(void);
    private:
        std::string m_str;
};

#endif

#include "Base.hpp"
#include <iostream>

using namespace std;

Base::Base(string str):m_str(std::move(str))
{
}

void Base::ShowString(void)
{
    std::cout << "Base::" << __func__ << m_str << std::endl;
}

#ifndef DERIVED_H_
#define DERIVED_H_

#include "Base.hpp"
#include <string>

class Derived : public Base
{
    public:
        Derived(std::string str);
        void Show(void);
    private:
        std::string m_str;
};

#endif

#include "Derived.hpp"
#include <iostream>
#include <memory>

using namespace std;

Derived::Derived(std::string str):Base(str), m_str(str)
{
}

void Derived::Show()
{
    std::unique_ptr<Base> B = std::make_unique<Base>(m_str);
    B->ShowString();
}

UT代码在这里: BaseMock.hpp

#ifndef BASEMOCK_H_
#define BASEMOCK_H_

#include "Base.hpp"

class BaseMock : public Base
{
    public:
        MOCK_METHOD0(ShowString, void());
};

#endif

UT_Derived.cpp

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "BaseMock.hpp"
#include "Derived.hpp"
#include <memory>

using ::testing::_;
using ::testing::Return;

using namespace std;
const std::string dummyStr { "dummyString" };

class UT_Derived : public ::testing::Test
{
    public:
        void SetUp()
        {
            derivedObj = std::make_shared<Derived>(dummyStr);
        }
    protected:
        std::unique_ptr<::testing::StrictMock<BaseMock>> baseMock =
            std::make_unique<::testing::StrictMock<BaseMock>>(dummyStr);
        std::shared_ptr<Derived> derivedObj;
};

TEST_F(UT_Derived, derivedShow)
{
    EXPECT_CALL(*baseMock, ShowString());
    derivedObj->Show();
}

int main(int argc, char** argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

运行测试: g ++-墙-g -pthread UT_Derived.cpp -lgtest_main -lgtest -lpthread

0 个答案:

没有答案