質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

1回答

5089閲覧

ローカル変数として生成されるインスタンスはモックで置き換えられないのでしょうか?

tacchang

総合スコア15

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

1クリップ

投稿2018/12/16 02:08

編集2018/12/16 04:46

GoogleMockでもCppUMockでも、フレームワークはどちらでもいいんですが、ローカル変数として生成されるインスタンスをモックに置き換えられずに困っています。

例えば、以下のような自分で作ったコード(ユニットテスト対象)があったとして、ConcreteCalculatorは他人が作る部位で未完成のため、自分で作っているAddition::add()のユニットテストでは、calc.add関数をモックで置き換えたいのです。

cpp:テスト対象

1#include "target.h" 2#include "concrete_calculator.h" 3 4int Addition::add(const int a, const int b) { 5 ConcreteCalculator calc; 6 return calc.add(a, b); 7}

ユニットテストの実行ファイルをビルド(リンク)する際、ConcreteCalculatorの実態を求められてしまいます。回避するいいアイデアはないでしょうか?

以下、CppUMockを使った場合のサンプルコードです。

text

1ソースコード 2. 3├── include 4│   ├── calculator.h 5│   ├── concrete_calculator.h 減っだだけ先に入手して仮置き 6│   └── target.h 7├── src 8│   └── target.cpp 9└── tests 10 ├── calculator-mock.h 11 └── test_target.cpp

cpp

1calculator.h 2#ifndef CALCULATOR_H 3#define CALCULATOR_H 4 5class Calculator { 6public: 7 virtual int add(const int x, const int y) = 0; 8 virtual ~Calculator() {} 9}; 10 11#endif //CALCULATOR_H

cpp

1concreate_calculator.h 2#ifndef CONCRETE_CALCULATOR_H 3#define CONCRETE_CALCULATOR_H 4 5#include "calculator.h" 6 7class ConcreteCalculator : public Calculator { 8public: 9 int add(const int x, const int y); 10}; 11 12#endif //CONCRETE_CALCULATOR_H

cpp

1target.h 2#ifndef TARGET_H 3#define TARGET_H 4 5#include "calculator.h" 6 7class Addition { 8public: 9 int add(const int a, const int b); 10}; 11 12#endif //TARGET_H

cpp

1target.cpp 2#include "target.h" 3#include "concrete_calculator.h" 4 5int Addition::add(const int a, const int b) { 6 ConcreteCalculator calc; 7 return calc.add(a, b); 8}

cpp

1calculator-mock.h 2#ifndef CALCULATOR_MOCK_H 3#define CALCULATOR_MOCK_H 4 5#include "CppUTestExt/MockSupport.h" 6 7#include "calculator.h" 8 9class CalculatorMock : public Calculator { 10public: 11 ~CalculatorMock() {} 12 int add(const int x, const int y) { 13 14 return mock().actualCall("add").withIntParameter("x", 10).onObject(this).returnIntValue(); // デタラメ 15 } 16}; 17 18#endif //CALCULATOR_MOCK_H

cpp

1test_target.cpp 2#include "CppUTest/CommandLineTestRunner.h" 3#include "CppUTestExt/MockSupport.h" 4#include "target.h" 5#include "calculator-mock.h" 6 7TEST_GROUP(TestFuncGroup) { 8 TEST_SETUP() { 9 10 } 11 12 TEST_TEARDOWN() { 13 mock().clear(); // Mock資源をクリア 14 } 15}; 16 17TEST(TestFuncGroup, Test1) 18{ 19 Calculator *calc = new CalculatorMock(); 20 21 mock().expectOneCall("add").onObject(calc); // デタラメ 22 const int a = 1; 23 const int b = 2; 24 const int expect = a + b; 25 Addition adder; 26 CHECK_EQUAL(expect, adder.add(a, b)); 27 mock().checkExpectations(); 28 29 delete calc; 30} 31 32int main(int argc, char **argv) { 33 // テストランナー 34 return RUN_ALL_TESTS(argc, argv); 35}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cateye

2018/12/16 04:11

タグがCになってますがC++ですよね?
tacchang

2018/12/16 04:42

おっと、ご指摘のとおりです。ありがとうございます。
guest

回答1

0

C++

1 Calculator* calc = new Calculatorから導出したニセモノ(); 2 return calc->add(a, b); 3 delete calc;

...ではダメっすかね?
Addition内部のCalculatorを差し替えたいなら、差し替えられるよう設計/実装せんと。

C++

1class Addition { 2private: 3 Calculator* calc_; 4public: 5 // construnctor injection 6 Addition(Calculator* calc) : calc_(calc) {} 7 8 int add(const int a, const int b) { calc_->add(a,b); } 9};

とかなんとか。

投稿2018/12/16 04:50

編集2018/12/16 12:24
episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問