MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"FOO",IsLessThan(yesterday)),234);

testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"BAR",yesterday),765);
testCase.assignOutputsWhen(dataServiceBehavior.lookupPrice(...
"BAR",IsLessThan(yesterday)),543);

You can now call the mocked lookupPrice method.

p1 = stubDataService.lookupPrice("FOO",yesterday)
p2 = stubDataService.lookupPrice("BAR",yesterday-days(5))

p1 =

123

p2 =

543

While the assignOutputsWhen method on testCase is convenient to specify behavior,
there is more functionality if you use the AssignOutputs action. For more information,
see “Specify Mock Object Behavior” on page 33-209.

Create Spy to Intercept Messages

Create a mock for the broker dependency and examine the methods on it. Since the
broker mock is used to verify interactions with the component under test (the trader
function), it exhibits spying behavior. The broker mock has an implicit interface. While the
buy method is not currently implemented, you can create a mock with it.

[spyBroker,brokerBehavior] = createMock(testCase,'AddedMethods',{'buy'});
methods(spyBroker)

Methods for class matlab.mock.classes.Mock:

buy

Call the buy method of the mock. By default it returns empty.

33 Unit Testing

Free download pdf