Class: NeAPI::Testing::FakeMaster
- Inherits:
-
Object
- Object
- NeAPI::Testing::FakeMaster
- Defined in:
- lib/ne_api/testing/fake_master.rb
Overview
NeAPI::Master のスタブクラス
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#access_token_end_date ⇒ Object
Returns the value of attribute access_token_end_date.
-
#call_history ⇒ Object
readonly
Returns the value of attribute call_history.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#refresh_token_end_date ⇒ Object
Returns the value of attribute refresh_token_end_date.
-
#retry_num ⇒ Object
Returns the value of attribute retry_num.
-
#wait_flag ⇒ Object
Returns the value of attribute wait_flag.
-
#wait_interval ⇒ Object
Returns the value of attribute wait_interval.
Instance Method Summary collapse
-
#call_count(method_name) ⇒ Integer
指定メソッドの呼び出し回数を取得.
-
#called?(method_name) ⇒ Boolean
指定メソッドが呼び出されたかチェック.
-
#clear_error ⇒ self
エラーモードを解除する.
-
#force_import ⇒ Object
強制インポートモード(本物と同じインターフェース).
-
#initialize(access_token: "test_access_token", refresh_token: "test_refresh_token") ⇒ FakeMaster
constructor
A new instance of FakeMaster.
-
#last_call_args(method_name) ⇒ Hash?
指定メソッドの最後の呼び出し引数を取得.
- #method_missing(method_name, args = {}) ⇒ Object
-
#reset! ⇒ self
全スタブと呼び出し履歴をクリアする.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#simulate_error(code: "001001", message: "API Error") ⇒ self
エラーモードを有効にする.
-
#stub(method_name, response = nil, &block) ⇒ self
特定メソッドのレスポンスをスタブする.
Constructor Details
#initialize(access_token: "test_access_token", refresh_token: "test_refresh_token") ⇒ FakeMaster
Returns a new instance of FakeMaster.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ne_api/testing/fake_master.rb', line 19 def initialize(access_token: "test_access_token", refresh_token: "test_refresh_token") @access_token = access_token @refresh_token = refresh_token @wait_flag = false @retry_num = 10 @wait_interval = 15 @stubbed_responses = {} @call_history = [] @error_mode = false @error_code = nil = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, args = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ne_api/testing/fake_master.rb', line 105 def method_missing(method_name, args = {}) @call_history << { method: method_name.to_sym, args: args, time: Time.now } if @error_mode raise NeAPIException, "#{@error_code}:#{@error_message}" end if @stubbed_responses.key?(method_name.to_sym) result = @stubbed_responses[method_name.to_sym].call(args) wrap_response(result, method_name) else default_response(method_name, args) end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
15 16 17 |
# File 'lib/ne_api/testing/fake_master.rb', line 15 def access_token @access_token end |
#access_token_end_date ⇒ Object
Returns the value of attribute access_token_end_date.
16 17 18 |
# File 'lib/ne_api/testing/fake_master.rb', line 16 def access_token_end_date @access_token_end_date end |
#call_history ⇒ Object (readonly)
Returns the value of attribute call_history.
17 18 19 |
# File 'lib/ne_api/testing/fake_master.rb', line 17 def call_history @call_history end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
15 16 17 |
# File 'lib/ne_api/testing/fake_master.rb', line 15 def refresh_token @refresh_token end |
#refresh_token_end_date ⇒ Object
Returns the value of attribute refresh_token_end_date.
16 17 18 |
# File 'lib/ne_api/testing/fake_master.rb', line 16 def refresh_token_end_date @refresh_token_end_date end |
#retry_num ⇒ Object
Returns the value of attribute retry_num.
15 16 17 |
# File 'lib/ne_api/testing/fake_master.rb', line 15 def retry_num @retry_num end |
#wait_flag ⇒ Object
Returns the value of attribute wait_flag.
15 16 17 |
# File 'lib/ne_api/testing/fake_master.rb', line 15 def wait_flag @wait_flag end |
#wait_interval ⇒ Object
Returns the value of attribute wait_interval.
15 16 17 |
# File 'lib/ne_api/testing/fake_master.rb', line 15 def wait_interval @wait_interval end |
Instance Method Details
#call_count(method_name) ⇒ Integer
指定メソッドの呼び出し回数を取得
88 89 90 |
# File 'lib/ne_api/testing/fake_master.rb', line 88 def call_count(method_name) @call_history.count { |c| c[:method] == method_name.to_sym } end |
#called?(method_name) ⇒ Boolean
指定メソッドが呼び出されたかチェック
80 81 82 |
# File 'lib/ne_api/testing/fake_master.rb', line 80 def called?(method_name) @call_history.any? { |c| c[:method] == method_name.to_sym } end |
#clear_error ⇒ self
エラーモードを解除する
57 58 59 60 61 62 |
# File 'lib/ne_api/testing/fake_master.rb', line 57 def clear_error @error_mode = false @error_code = nil = nil self end |
#force_import ⇒ Object
強制インポートモード(本物と同じインターフェース)
101 102 103 |
# File 'lib/ne_api/testing/fake_master.rb', line 101 def force_import @wait_flag = true end |
#last_call_args(method_name) ⇒ Hash?
指定メソッドの最後の呼び出し引数を取得
96 97 98 |
# File 'lib/ne_api/testing/fake_master.rb', line 96 def last_call_args(method_name) @call_history.reverse.find { |c| c[:method] == method_name.to_sym }&.[](:args) end |
#reset! ⇒ self
全スタブと呼び出し履歴をクリアする
67 68 69 70 71 72 73 74 |
# File 'lib/ne_api/testing/fake_master.rb', line 67 def reset! @stubbed_responses.clear @call_history.clear @error_mode = false @error_code = nil = nil self end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
120 121 122 |
# File 'lib/ne_api/testing/fake_master.rb', line 120 def respond_to_missing?(method_name, include_private = false) true end |
#simulate_error(code: "001001", message: "API Error") ⇒ self
エラーモードを有効にする
47 48 49 50 51 52 |
# File 'lib/ne_api/testing/fake_master.rb', line 47 def simulate_error(code: "001001", message: "API Error") @error_mode = true @error_code = code = self end |
#stub(method_name, response = nil, &block) ⇒ self
特定メソッドのレスポンスをスタブする
37 38 39 40 |
# File 'lib/ne_api/testing/fake_master.rb', line 37 def stub(method_name, response = nil, &block) @stubbed_responses[method_name.to_sym] = block || -> (_) { response } self end |