MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

s1 = spyBroker.buy
s2 = spyBroker.buy("inputs",[13 42])


s1 =


[]


s2 =


[]


Since the trader function does not use the status return code, the default mock behavior
of returning empty is acceptable. The broker mock is a pure spy, and does not need to
implement any stubbing behavior.


Call Component Under Test


Call the trader function. In addition to the ticker symbol and the number of shares to
buy, the trader function takes as inputs the data service and the broker. Instead of
passing in actual data service and broker objects, pass in the spyBroker and
stubDataService mocks.


trader(stubDataService,spyBroker,"FOO",100)
trader(stubDataService,spyBroker,"FOO",75)
trader(stubDataService,spyBroker,"BAR",100)


Verify Function Interactions


Use the broker behavior object (the spy) to verify that the trader function calls the buy
method, as expected.


Use the TestCase.verifyCalled method to verify that the trader function instructed
the buy method to buy 100 shares of the FOO stock.


import matlab.mock.constraints.WasCalled;
testCase.verifyCalled(brokerBehavior.buy("FOO",100))


Interactive verification passed.


Verify that FOO stock was purchased two times, regardless of the specified number of
shares. While the verifyCalled method is convenient to specify behavior, there is more


Create Mock Object
Free download pdf