Mời các bạn xem danh sách tổng hợp Mock là gì hay nhất được tổng hợp bởi chúng tôi
Depended on Components
In this example, the component under test is a simple day-trading algorithm. It is the part of the system you want to test independent of other components. The day-trading algorithm has two dependencies: a data service to retrieve the stock price data and a broker to purchase the stock.
In a file DataService.m in your current working folder, create an abstract class that includes a lookupPrice method.
In production code, there could be several concrete implementations of the DataService class, such as a BloombergDataService class. This class uses the Datafeed Toolbox™. However, since we create a mock of the DataService class, you do not need to have the toolbox installed to run the tests for the trading algorithm.
In this example, assume that the broker component has not been developed yet. Once it is implemented, it will have a buy method that accepts a ticker symbol and a specified number of shares to buy, and returns a status code. The mock for the broker component uses an implicit interface, and does not derive from a superclass.
Component Under Test
In a file trader.m in your current working folder, create a simple day trading algorithm. The trader function accepts as inputs a data service object that looks up the price of the stock, a broker object that defines how the stock is bought, a ticker symbol, and a number of shares to purchase. If the price from yesterday is less than the price two days ago, instruct the broker to buy the specified number of shares.
Mock Objects and Behavior Objects
The mock object is an implementation of the abstract methods and properties of the interface specified by a superclass. You can also construct a mock without a superclass, in which case the mock has an implicit interface. The component under test interacts with the mock object, for example, by calling a mock object method or accessing a mock object property. The mock object carries out predefined actions in response to these interactions.
When you create a mock, you also create an associated behavior object. The behavior object defines the same methods as the mock object and controls mock behavior. Use the behavior object to define mock actions and qualify interactions. For example, use it to define values a mocked method returns, or verify that a property was accessed.
At the command prompt, create a mock test case for interactive use. Using the mock in a test class instead of at the command prompt is presented later in this example.
Create Stub to Define Behavior
Create a mock for the data service dependency and examine the methods on it. The data service mock returns predefined values, replacing the implementation of the service that provides actual stock prices. Therefore, it exhibits stubbing behavior.
In the DataService class, the lookupPrice method is abstract and static. The mocking framework implements this method as concrete and static.
Define behavior for the data service mock. For ticker symbol “FOO”, it returns the price yesterday as $123 and anything before yesterday is $234. Therefore, according to the trader function, the broker always buys stock “FOO”. For the ticker symbol “BAR”, it returns the price yesterday as $765 and anything before yesterday is $543. Therefore, the broker never buys stock “BAR”.
You can now call the mocked lookupPrice method.
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.
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.
Call the buy method of the mock. By default it returns empty.
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.
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.
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 functionality if you use the WasCalled constraint. For example, you can verify that a mocked method was called a specified number of times.
Verify that the buy method was not called requesting 100 shares of the BAR stock.
Although the trader function was called requesting 100 shares of BAR stock, the stub defined yesterday’s price for BAR to return a higher value than all days prior to yesterday. Therefore, the broker never buys stock “BAR”.
Test Class for trader Function
The interactive test case is convenient to experiment with at the command prompt. However, it is typical to create and use mocks within a test class. In a file in your current working folder, create the following test class that incorporates the interactive testing from this example.
Run the tests and view a table of the results.