or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
U dk Stub & Mock meh??
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Stub
v.s.Mock
2022-10-25 Ting
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →slide: https://hackmd.io/@tingtinghsu/S1s64axEj#/
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →mock
can be replaced bystub
Challenges for new stars
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →1. Test Double
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Ref: 測試中常見的名詞:Stub, Dummy, Mock..等
Why we need Test Doubles?
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Why we need Test Doubles?
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Fixtures and Fatories
Test Double
Benefits of Test Doubles
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Test doubles allow tests to proceed by
faking the method calls
that produce the data (instead of faking data)Use
cheaper
ormore focussed replacement
for a more expensive real object- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →2. Stub Objects
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →test/helpers/model_helper.rb
stubs - a common helper to testing environments(Martin Fowler - Mocks Aren't Stubs)
To provide
canned answer to calls made
during the test. (wiki)A fake object that returns a canned value for a method call
without calling the actual method on an actual object.
Stub: returns canned value
Let's read Stub in codebase
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →codebase usage: stub (1)
test/models/master_plan_test.rb#L25
codebase usage: stub (2)
test/controllers/web/b2b/applications_controller_test.rb#L41
test/helpers/model_helper.rb#L162 def self.stub_has?(hash)
codebase usage: stub (3-1)
test/models/ability_web_client_test.rb#L180
codebase usage: stub (3-2)
test/models/ability_web_client_test.rb#L19
codebase usage: stub (3-3)
test/forms/service_record_item/reduce_contribution_form_test.rb#L33
test/helpers/model_helper.rb#L73 def stub_any_instance
codebase usage: stub (4)
test/forms/service_record_item/add_single_topup_form_test.rb#L56
test/helpers/model_helper.rb#L169 def self.begin_stub_has?(hash)
3. Mock Objects
return the fake value
& set a testable expection thatthe replaced method will actually be called
in the testDOC (Depended-On Component) :想抽換掉的相依性元件
stub method in Minitest
it
overrides
a single method for the duration of the blockwe want to
make sure we called the replaced method
, but we don’t care to test if it works or not (because it’s already been tested)If the replaced method is not called, the mock object
triggers a test failure
Mock usage (1) - name & retval
https://github.com/minitest/minitest/blob/master/lib/minitest/mock.rbL56
mock.expect(:call, "expect return value when the method_name is called")
Mock usage (2) - [args]
mock.expect(:call, "expect return value when the method_name is called", [丟入的參數])
Mock usage (3)
do block
後接block的方式指定參數
https://github.com/minitest/minitest/blob/master/lib/minitest/mock.rb#L91
Mock usage (4)
+args+
+args+
is compared to the expected args using case quality (===
), allowing for less specific expectation.https://github.com/minitest/minitest/blob/master/lib/minitest/mock.rb#L199
Mock usage (5)
when a method is called multiple times
Specify a new expect for each one. They will be used in the order you define them
https://github.com/minitest/minitest/blob/master/lib/minitest/mock.rb#L163
4. Mock & Stub combination
Explanation from John dada
Let's read Mock in codebase
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Mock in codebase (1)
mock.expect(:call, "expect return value when the method_name is called")
test/forms/service_record_item/cancel_contribution_holiday_form_test.rb#L45 vs. 實作L15
Mock usage (2) - [args]
mock.expect(:call, "expect return value when the method_name is called", [丟入的參數])
test/forms/service_record_item/pending_payment_info_form/cc_aw_test.rb#L24 vs. 實作L33
Mock usage (3) - 後接block 指定參數
test/forms/service_record_item/adjust_override_due_on_form_test.rb#L69 vs. 實作L32
Mock usage (4) -
+args+
test/forms/service_record_item/personal_data_change_form_test.rb#L453 v.s. 實作 app/repositories/internal/client_repo.rb#L120 vs. test/repositories/internal/client_repo_test.rb#L141
Mock usage (5)
mock called multiple times
test/services/change_initial_contribution_service_test.rb#L212 vs. 實作L90
如果沒有call正確的次數, 會raises MockExpectationError
discussion time:
When
mock
can be replaced bystub
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →When
Image Not Showing
Possible Reasons
mock
is better thanstub
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Slack Discussion in Feb. 2019
Mock
時會強調驗證傳入的參數
Wrap up
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Stub
on a method ispassive
Mock
on a mthod isaggressive
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →給新星
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →多寫測試
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →查GitHub文件
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →slack討論串挖寶
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →比較codebase 各種 stub/ Mock用法,想想有沒有其他更好的方式
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →多Google,多讀幾遍 ⬇️ reference
Thank you!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Ref:
Minitest GitHub
Martin Fowler -Mocks Aren't Stubs
mocking in ruby with minitest
測試中常見的名詞:Stub, Dummy, Mock..等