私はあなたの助けが必要な非仮想メソッドを嘲笑する問題に遭遇しています。 私はリンクを参照しました:Mock non-virtual method giving compilation errorGmockでCppの別の非仮想メソッドで非仮想メソッドをモック
私は彼らが何をしたのか理解しました。しかし、私は高度な質問があります。私が持っていると仮定します
*/
#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using ::testing::Invoke;
using ::testing::NiceMock;
template <class myclass> class Templatemyclass {
public:
myclass T;
void show() ;
};
template <class myclass> void Templatemyclass<myclass>::show()
{
T.show_text();
}
struct Test {
void display() { std::cout << __func__<<":-->Inside the display Test\n"; }
void show_text() {
display(); // HOW to re-route it to my_display() ? (>.<)
}
};
struct MockTest {
MOCK_CONST_METHOD0(display, void());
MOCK_CONST_METHOD0(show_text, void());
};
void my_display(){
{ std::cout <<__func__<<":-->Outside the display Test\n"; }
}
int main() {
//NiceMock<Templatemyclass<Test> > obj1;
//obj1.show();
NiceMock<Templatemyclass<MockTest> > obj2;
EXPECT_CALL(obj2.T, display())
.Times(1)
.WillOnce(Invoke(my_display));
obj2.show();
return 0;
}
私はshow_text
が呼び出されたとき、display
を呼び出すために起こっていることをしたいと思います。私はdisplay
をモックしてmy_display
に再ルーティングしようとしましたが、失敗しました。私はエラーが発生しました。
../test_program.cpp:56: Failure
Actual function call count doesn't match EXPECT_CALL(obj2.T, display())...
Expected: to be called once
Actual: never called - unsatisfied and active
上記のソースコードを少し変更する。それは働くことができるが、これは私の期待を逸した。私は模擬display
show_text
で呼びかけたいと思います。
template <class myclass> class Templatemyclass {
public:
myclass T;
void show() ;
};
template <class myclass> void Templatemyclass<myclass>::show()
{
T.display();
}
struct Test {
void display() { std::cout << __func__<<":-->Inside the display Test\n"; }
// void show_text() {
// display(); // HOW to re-route it to my_display() ? (>.<)
// }
};
struct MockTest {
MOCK_CONST_METHOD0(display, void());
MOCK_CONST_METHOD0(show_text, void());
};
void my_display(){
{ std::cout <<__func__<<":-->Outside the display Test\n"; }
}
int main() {
NiceMock<Templatemyclass<Test> > obj1;
obj1.show();
NiceMock<Templatemyclass<MockTest> > obj2;
EXPECT_CALL(obj2.T, display())
.Times(1)
.WillOnce(Invoke(my_display));
obj2.show();
return 0;
}
、画面を示しています。
display:-->Inside the display Test
my_display:-->Outside the display Test
私は、この問題をクリアする助けてください。
GMockのドキュメントを読むと、すべての模擬クラスに仮想インターフェイスが必要であると言われましたか? –
可能です、Richard。あなたは私の編集されたポストを見てみることができます –