2013-03-20 86 views
3

我是新來的GTEST,只是瞭解如何素作品,GoogleTest期待致電失敗的模擬方法

我試着寫了簡單的程序了foo.h和FooDisplay.h(這需要在富構造),也MockFoo.cpp(這是主要GTEST程序來測試Gmock)..

當我嘲笑並執行期望調用爲Foo,它下面拋出在執行..

日誌

[==========] Running 1 test from 1 test case. 
[----------] Global test environment set-up. 
[----------] 1 test from Foo 
[ RUN  ] Foo.case1 
package/web/webscr/MockFoo.cpp:19: Failure 
Actual function call count doesn't match EXPECT_CALL(mock, GetSize(_))... 
     Expected: to be called once 
      Actual: never called - unsatisfied and active 
[ FAILED ] Foo.case1 (0 ms) 
[----------] 1 test from Foo (0 ms total) 

[----------] Global test environment tear-down 
[==========] 1 test from 1 test case ran. (0 ms total) 
[ PASSED ] 0 tests. 
[ FAILED ] 1 test, listed below: 
[ FAILED ] Foo.case1 

foo.h中

#include <gtest/gtest.h> 
#include <infra/utility/core/lang/PPException.h> 
#include <infra/utility/core/lang/String.h> 
#include <iostream> 

class Foo { 

     public: 
       virtual ~Foo() 
       { 

       } 

       virtual int GetSize(int size) 
       { 
         return size; 
       } 

}; 

FooDisplay.h

#include <gtest/gtest.h> 
#include <infra/utility/core/lang/PPException.h> 
#include <infra/utility/core/lang/String.h> 
#include <iostream> 
#include "Foo.h" 

class FooDisplay { 

    public: 
       FooDisplay (Foo _foo) : foo(_foo) 
       { 

       } 
     virtual ~FooDisplay() 
     { 

     } 

     virtual String Describe(int size) 
     { 
      foo.GetSize(size); 
      String str = "Done"; 
      return str; 
     } 
     private: 
       Foo foo; 
}; 

MooFoo.cpp

#include "gmock/gmock.h" 
#include <infra/utility/core/lang/String.h> 
#include <iostream> 
#include "FooDisplay.h" 

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

class MockFoo : public Foo { 
public: 
    MOCK_CONST_METHOD1(GetSize, int(int size)); 
    //MOCK_CONST_METHOD1(Describe, String(int)); 
}; 

TEST(Foo,case1) 
{ 
     MockFoo mockfoo; 
     FooDisplay Display(mockfoo); 

     EXPECT_CALL(mockfoo,GetSize(_)).WillOnce(Return(6)); 
     Display.Describe(5); 
//  ON_CALL(Display,Describe(5)).WillByDefault(Return ("Hi")); 
} 

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

回答

4

您的代碼中存在兩個錯誤,這意味着您缺少C++基礎知識。

  1. 第一個bug在你的模擬類中。在基類FooGetSize()方法聲明爲:
    virtual int GetSize(int size)
    但在你的模擬,它是:
    MOCK_CONST_METHOD1(GetSize, int(int size));
    意味着
    virtual int GetSize(int size) const
    這兩種方法有不同的特徵,正因爲如此,MockFoo不覆蓋Foo的方法。

  2. 第二個錯誤是當你將MockFoo的對象傳入DisplayFoo的構造函數。我認爲你想要做的是使用MockFoo對象,但是你不小心複製了它的基礎對象。更改FooDisplay這應該解決這個問題:

    class FooDisplay { 
    public: 
        FooDisplay (Foo & _foo) : foo (_foo) { } 
        virtual ~FooDisplay() { } 
    
        virtual std::string Describe (int size) { 
         foo.GetSize (size); 
         std::string str = "Done"; 
         return str; 
        } 
    
        private: 
         Foo & foo; 
    }; 
    
+0

冷卻這是工作的感謝... – Gopal 2013-03-20 08:21:26