Class: NeAPI::Testing::FakeMaster

Inherits:
Object
  • Object
show all
Defined in:
lib/ne_api/testing/fake_master.rb

Overview

NeAPI::Master のスタブクラス

Examples:

基本的な使い方

fake = NeAPI::Testing::FakeMaster.new
fake.stub(:master_goods_search, [{ "goods_id" => "001" }])
fake.master_goods_search  #=> [{ "goods_id" => "001" }]

エラーのシミュレート

fake.simulate_error(code: "001001", message: "認証エラー")
fake.master_goods_search  #=> raises NeAPIException

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  @error_message = 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_tokenObject

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_dateObject

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_historyObject (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_tokenObject

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_dateObject

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_numObject

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_flagObject

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_intervalObject

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

指定メソッドの呼び出し回数を取得

Parameters:

  • method_name (Symbol, String)

    メソッド名

Returns:

  • (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

指定メソッドが呼び出されたかチェック

Parameters:

  • method_name (Symbol, String)

    メソッド名

Returns:

  • (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_errorself

エラーモードを解除する

Returns:

  • (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
  @error_message = nil
  self
end

#force_importObject

強制インポートモード(本物と同じインターフェース)



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?

指定メソッドの最後の呼び出し引数を取得

Parameters:

  • method_name (Symbol, String)

    メソッド名

Returns:

  • (Hash, nil)


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

全スタブと呼び出し履歴をクリアする

Returns:

  • (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
  @error_message = nil
  self
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (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

エラーモードを有効にする

Parameters:

  • code (String) (defaults to: "001001")

    エラーコード

  • message (String) (defaults to: "API Error")

    エラーメッセージ

Returns:

  • (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
  @error_message = message
  self
end

#stub(method_name, response = nil, &block) ⇒ self

特定メソッドのレスポンスをスタブする

Parameters:

  • method_name (Symbol, String)

    スタブするメソッド名

  • response (Object) (defaults to: nil)

    返却するレスポンス(ブロックも可)

Returns:

  • (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