Google Mock: тестирование определенного свойства объекта - PullRequest
2 голосов
/ 06 апреля 2019

Я пытаюсь определить пользовательское сопоставление, следуя примеру, приведенному в 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) встроенный оператор bool == (faketype, faketype) {return true;}

.... gtest \ gtest.h: 14096: 13: примечание: нет известного преобразования для аргумента 1 из 'const testing :: Matcher' в 'testing :: internal:: faketype '

Как мне решить эти ошибки?

Спасибо

1 Ответ

1 голос
/ 10 апреля 2019

Сопоставитель должен использоваться только в вашем методе тестирования, а не в определениях классов. 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);
}

Кстати, вы также можете избежать проблем с определением полного интерфейса и выполнить то же самое с помощью простого пользовательского сопоставления , например:

MATCHER_P(BarPlusBazEq, expected_sum, "") { return arg.bar() + arg.baz() == expected_sum; }
...