Main Content

matlab.mock.constraints.WasAccessed class

Package: matlab.mock.constraints

Constraint determining property get access

Description

The WasAccessed constraint produces a qualification failure if an actual value is not a PropertyBehavior instance, or if the property that corresponds to the PropertyBehavior was not accessed the specified number of times.

The number of times a property is accessed includes the number of times that MATLAB® implicitly accesses the property. For example, if you display a mock object, MATLAB accesses the object properties to display their values.

Construction

constraint = WasAccessed provides a constraint that determines the property get access. If a property was accessed at least once, the constraint is satisfied. To qualify that a property was not accessed, negate the WasAccessed constraint with the tilde (~) operator.

constraint = WasAccessed('WithCount',n) provides a constraint that is satisfied when a property was accessed exactly n times.

If you negate WasAccessed with this syntax, if the property was not accessed exactly n times, the constraint passes. For example, if a property was accessed 4 times, ~WasAccessed('WithCount',3) passes and ~WasAccessed('WithCount',4) fails.

Input Arguments

expand all

Number of times of property get access, specified as an integer.

Properties

expand all

Property access count, returned as an integer. This property is read-only once the constraint is constructed. You can specify it during constraint construction.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Create a mock for a person class.

testCase = matlab.mock.TestCase.forInteractiveUse;
[fakePerson,behavior] = testCase.createMock('AddedProperties',["Name" "Age"]);
fakePerson.Name = 'David';
fprintf(1,'The person''s name is %s.\n',fakePerson.Name);
The person's name is David.

Construct passing cases.

import matlab.mock.constraints.WasAccessed
testCase.verifyThat(behavior.Name,WasAccessed)
Interactive verification passed.
testCase.verifyThat(behavior.Age,~WasAccessed)
Interactive verification passed.
testCase.verifyThat(behavior.Name,WasAccessed('WithCount',1))
Interactive verification passed.

Construct failing cases.

testCase.verifyThat(behavior.Name,~WasAccessed)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
Negated WasAccessed failed.
--> Property 'Name' was unexpectedly accessed 1 time(s).

Specified property access:
    PropertyGetBehavior
        <Mock>.Name
testCase.verifyThat(behavior.Age,WasAccessed)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasAccessed failed.
--> Property 'Age' was never accessed.

Specified property access:
    PropertyGetBehavior
        <Mock>.Age
testCase.verifyThat(behavior.Name,WasAccessed('WithCount',5))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasAccessed failed.
--> Property 'Name' was not accessed the expected number of times.
    
    Actual property access count:
             1
    Expected property access count:
             5

Specified property access:
    PropertyGetBehavior
        <Mock>.Name

Version History

Introduced in R2017a