Google Mock:测试对象的某个属性

问题描述 投票:2回答:1

我尝试按照Google Mock CookBook中提供的示例定义自定义匹配器。

代码如下

    #include "gmock/gmock.h"
    #include "gtest/gtest.h"
    using namespace std;
    using ::testing::MatcherInterface;
    using ::testing::MatchResultListener;
    using ::testing::Matcher;
    using ::testing::_;
    using ::testing::AtLeast;
    using ::testing::Invoke;
    class Foo2;
    class Foo2
    {
        public:
            virtual int bar() const
            {
                return 4;
            }
            virtual int baz() const
            {
                return 5;
            }
            virtual void DoThis (Matcher<const Foo2&> pFunc)
          {
                     std::cout << "Foo:DoThis" << std::endl;
          }
          virtual void DoThat(int)
          {
                 std::cout << "Foo:DoThat" << std::endl;
          }
            virtual ~Foo2()
            {

            }

    };

    class BarPlusBazEqMatcher : public MatcherInterface<const Foo2&> {
     public:
      explicit BarPlusBazEqMatcher(int expected_sum)
          : expected_sum_(expected_sum) {}

      virtual bool MatchAndExplain(const Foo2& foo,
                                   MatchResultListener* listener) const {
        return (foo.bar() + foo.baz()) == expected_sum_;
      }

      virtual void DescribeTo(::std::ostream* os) const {
        *os << "bar() + baz() equals " << expected_sum_;
      }

      virtual void DescribeNegationTo(::std::ostream* os) const {
        *os << "bar() + baz() does not equal " << expected_sum_;
      }
      virtual ~BarPlusBazEqMatcher()
      {

      }

     private:
      const int expected_sum_;
    };


    inline Matcher<const Foo2&> BarPlusBazEq(int expected_sum) {
      return MakeMatcher(new BarPlusBazEqMatcher(expected_sum));
    }


    class MockFoo2 : public Foo2 {
     public:

      MOCK_METHOD1(DoThis,void(Matcher<const Foo2&>));
      MOCK_METHOD1(DoThat, void(int));
    };

    TEST(MockMatcher, Matcher)
    {
        MockFoo2 mockF;
        EXPECT_CALL(mockF, DoThis(BarPlusBazEq(5)));
    }

当我尝试编译上述代码时,会生成以下编译错误

.... gtest \ gtest.h:9160:60:错误:不匹配'operator =='(操作数类型是'const testing :: Matcher'和'const testing :: Matcher')bool operator()(const A&a,const B&b)const {return a == b; } ~~ ^ ~~~

.... gtest \ gtest.h:14096:13:注意:候选人:bool testing :: internal :: operator ==(testing :: internal :: faketype,testing :: internal :: faketype)inline bool operator == (faketype,faketype){return true; }

.... gtest \ gtest.h:14096:13:注意:参数1从'const testing :: Matcher'到'testing :: internal :: faketype'没有已知的转换

我该如何解决这些错误?

谢谢

c++ testing matcher googlemock
1个回答
1
投票

匹配器只应在测试方法中使用,而不能在类定义中使用。该示例中的BarPlusBazEq期望匹配const &Foo2类型的函数参数。因此需要将DoThis()定义为采用该参数类型的函数:

class Foo2 {
public:
    virtual int bar() const { return 4; }
    virtual int baz() const { return 5; }

    virtual void DoThis(const Foo2 &foo) {
        std::cout << "Foo2:DoThis" << std::endl;
    }
    virtual ~Foo2() {}
};

class MockFoo2 : public Foo2 {
public:
    MOCK_METHOD1(DoThis, void(const Foo2&));
};

class BarPlusBazEqMatcher : public MatcherInterface<const Foo2&> {
public:
    explicit BarPlusBazEqMatcher(int expected_sum)
        : expected_sum_(expected_sum) {}

    virtual bool MatchAndExplain(const Foo2& foo,
        MatchResultListener* listener) const {
        return (foo.bar() + foo.baz()) == expected_sum_;
    }

    virtual void DescribeTo(::std::ostream* os) const {
        *os << "bar() + baz() equals " << expected_sum_;
    }

    virtual void DescribeNegationTo(::std::ostream* os) const {
        *os << "bar() + baz() does not equal " << expected_sum_;
    }

    virtual ~BarPlusBazEqMatcher() {}

private:
    const int expected_sum_;
};

inline Matcher<const Foo2&> BarPlusBazEq(int expected_sum) {
    return MakeMatcher(new BarPlusBazEqMatcher(expected_sum));
}

TEST(MockMatcher, Matcher)
{
    MockFoo2 mock;
    EXPECT_CALL(mock, DoThis(BarPlusBazEq(9)));

    // You also have to do something to trigger the expected call...
    Foo2 foo2;
    mock.DoThis(foo2);
}

顺便说一句,你也可以避免定义一个完整的界面和用一个简单的custom matcher完成相同的事情的麻烦,如下所示:

MATCHER_P(BarPlusBazEq, expected_sum, "") { return arg.bar() + arg.baz() == expected_sum; }
© www.soinside.com 2019 - 2024. All rights reserved.