2017-05-22 121 views
1

比方說,你有這樣的:谷歌模擬 - 保存EXPECT_CALL然後重新使用和更改條款

EXPECT_CALL(MockClass_obj, f1(55)).Times(1); 
// use the expectation 
// ... 

// Now clear it 
Mock::VerifyAndClear(&MockClass_obj) 

有可能是到 1)保存預期

2)後重新使用它並更改條款?

here我知道這是可以節省expectations,但沒有其他地方說明還有什麼可以做。

參考上面的代碼,我想要做的事,如:

Expecatation exp1 = EXPECT_CALL(MockClass_obj, f1(55)).Times(1); 
// use the expectation 
// ... 

// Now clear it 
Mock::VerifyAndClear(&MockClass_obj) 

// Somehow modify exp1 to change the cardinality or any of the clauses 
// so that I can make the test immediately readable and convey that it's very much related to the one above": 

// Instead of this: 
EXPECT_CALL(MockClass_obj, f1(55)).Times(0); 

// I wanna do something like 
exp1.Times(0) 

回答

1

不,它在問題中描述的方式是不可能的。期望類僅用於After子句中,而不是其他任何東西 - 通過google-mock設計,您無法對其進行任何其他操作。

但是,適當的封裝 - 你可能達到的目標,如:

::testing::Expectation expectF1For55(::testing::Cardinality cardinality) 
{ 
    EXPECT_CALL(MockClass_obj, f1(55)).Times(cardinality); 
} 

及用途:

auto exp1 = expectF1For55(Exactly(1)); 
// ... 

// ... 
expectF1For55(Exactly(0)); 

但是我敢打賭,問題是更普遍的不僅僅是非常覆蓋這例。

E.g.你可以封裝整個期望 - 無論你需要什麼 - 這樣的:

class F1Expectation 
{ 
public: 
    F1Expectation(MockClass&); 
    Expectation expect(int arg, ::testing::Cardinality cardinality = Exaclty(1)) 
    { 
     m_arg = arg; 
     return EXPECT_CALL(obj, f1(m_arg)).Times(cardinality); 
    } 
    Expectation expect(::testing::Cardinality cardinality) 
    { 
     return EXPECT_CALL(obj, f1(55)).Times(cardinality); 
    } 
private: 
    MockClass& obj; 
    Matcher<int> m_arg = _; 
}; 

及用途:

F1Expectation exp1(MockClass_obj); 
exp1.expect(1); 
// ... 

// ... 
exp1.expect(0);