簡體   English   中英

使用boost :: shared_ptr和AMOP模擬

[英]Mocking using boost::shared_ptr and AMOP

我正在嘗試使用amop編寫模擬 我正在使用Visual Studio 2008。

我有這個接口類:

struct Interface {
   virtual void Activate() = 0;
};

另一個類接收此Interface指針,如下所示:

struct UserOfInterface {
   void execute(Interface* iface) {
      iface->Activate();
   }
};

因此,我嘗試編寫一些測試代碼,如下所示:

amop::TMockObject<Interface> mock;
mock.Method(&Interface::Activate).Count(1);

UserOfInterface user;
user.execute((Interface*)mock);

mock.Verifiy();

有用! 到目前為止一切順利,但是我真正想要的是execute()方法中的boost :: shared_ptr,所以我這樣寫:

struct UserOfInterface {
   void execute(boost::shared_ptr<Interface> iface) {
      iface->Activate();
   }
};

現在測試代碼應該如何? 我嘗試了一些方法,例如:

amop::TMockObject<Interface> mock;
mock.Method(&Interface::Activate).Count(1);

UserOfInterface user;
boost::shared_ptr<Interface> mockAsPtr((Interface*)mock);
user.execute(mockAsPtr);

mock.Verifiy();

它可以編譯,但顯然會崩潰,因為在作用域末尾,變量“ mock”被雙重破壞(由於堆棧變量“ mock”和shared_ptr)。

我還嘗試在堆上創建'mock'變量:

amop::TMockObject<Interface>* mock(new amop::TMockObject<Interface>);
mock->Method(&Interface::Activate).Count(1);

UserOfInterface user;
boost::shared_ptr<Interface> mockAsPtr((Interface*)*mock);
user.execute(mockAsPtr);

mock->Verifiy();

但是它不起作用,以某種方式進入了無限循環,直到我遇到了一個問題,即在shared_ptr嘗試刪除對象時,boost找不到對象的析構函數。

有人成功使用帶有boost :: shared_ptr的amop嗎?

您可能想嘗試使用更明確的演員表。 我不確定這是否行得通,但請嘗試一下。

// Get the mock generator
boost::shared_ptr< amop::TMockObject<Interface> > mock
    = boost::make_shared< amop::TMockObject<Interface> >;
// Get the mocked interface
boost::shared_ptr<Interface> imock = boost::dynamic_pointer_cast<Interface>(mock);

// Setup mock usage expectations
mock->Method(&Interface::Activate).Count(1);

// Run the test
UserOfInterface user;
user.execute(imock);

// Verify the expectations were met
mock->Verifiy();

好吧,我從來沒有使用過安培,但是這種增強模式可能會有所幫助。

要創建一個boost shared_ptr,您還可以使用

boost::shared_ptr<Interface> mock(new amop::TMockObject<Interface>());

這樣,就不會在堆棧上創建模擬對象,只有在shared_ptr中的引用計數器變為零時才將其銷毀。 因為這與您的第二次嘗試基本相同,所以另一個技巧是:

如果遇到看起來像c ++找不到正確的析構函數的問題,則可以在基(接口)類中引入虛擬析構函數。 amop允許嗎?

class Interface{
  virtual ~Interface() { }
  ...
};

有一種方法可以將shared_ptr與amop一起使用

struct Interface {
   virtual ~Interface() {}
   virtual void Activate() = 0;
};

TEST(MockObjectMethodDestructor)
{  
    TMockObject<Interface> mock;

    mock.Method(Destructor());

    boost::shared_ptr<Interface> ptr((IInterface*)mock);

    ptr.reset();
}

您可以給shared_ptr一個自定義函數,當引用計數為零時將調用該自定義函數,而不是刪除函數。

然后,代碼將如下所示(我沒有嘗試對其進行編譯):

struct NoOpDel
{
void operator() (void *) { }
}

amop::TMockObject<Interface> mock;
mock.Method(&Interface::Activate).Count(1);

UserOfInterface user;
boost::shared_ptr<Interface> imock((Interface*)mock, NoOpDel())
user.execute(imock);

mock.Verify();

有關更多詳細信息,請參見Boost API文檔 ,您對此構造函數感興趣:

template<class Y, class D> shared_ptr(Y * p, D d);

免責聲明:我是HippoMocks的作者

使用HippoMocks,您可以指定希望在測試結束時調用析構函數。 在測試結束時,它還隱式地驗證了您的期望。 這樣,它甚至可以防止在您忘記刪除的堆上有一個雜散的shared_ptr或在您不使用shared_ptr的情況下不會擁有所有權或忘記刪除指針的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM