当前位置:网站首页>Spock sub piling

Spock sub piling

2022-06-23 07:15:00 qq_ thirty-seven million two hundred and seventy-nine thousand

Stubbing It is the behavior that enables the collaborator to respond to method calls in some way . When you stub a method , Don't care about the number of calls to this method , Just hope it returns some values when called , Or perform some reaction .

subscriber.receive(_) >> "ok"
|          |       |     |
|          |       |     response generator
|          |       argument constraint
|          method constraint
target constraint

 Such as :subscriber.receive(_) >> "ok"  meaning , No matter what instance , What parameters , call  receive  Methods return strings  ok

Return fixed value

Use >> Operator , Return fixed value

subscriber.receive(_) >> "ok"

Return value sequence

Return a sequence , Iterates and returns the specified value in turn . As shown below , The first call returns ok, The second call returns error, And so on

subscriber.receive(_) >>> ["ok", "error", "error", "ok"]

Calculate the return value dynamically

subscriber.receive(_) >> { args -> args[0].size() > 3 ? "ok" : "fail" }
subscriber.receive(_) >> { String message -> message.size() > 3 ? "ok" : "fail" }

Produce reaction

subscriber.receive(_) >> { throw new InternalError("ouch") }

Chain response

subscriber.receive(_) >>> ["ok", "fail", "ok"] >> { throw new InternalError() } >> "ok"
原网站

版权声明
本文为[qq_ thirty-seven million two hundred and seventy-nine thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230623344949.html