Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

matlab.mock.constraints.WasSet class

Package: matlab.mock.constraints

Constraint determining property set interaction

Description

The WasSet 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 set the specified number of times.

Construction

constraint = WasSet provides a constraint that determines property set interaction. If a property value was set at least once, the constraint is satisfied. To qualify that a property was not set, negate the WasSet constraint with the tilde (~) operator.

constraint = WasSet(Name,Value) provides a constraint with additional options specified by one or more Name,Value pair arguments. For example, WasSet('ToValue',42) constructs a constraint that is satisfied if a property value is set to 42, and WasSet('ToValue',42,'WithCount',3) constructs a constraint that is satisfied if a property value is set to 42 exactly 3 times.

Input Arguments

expand all

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example:

expand all

Specified property values, specified as a scalar, vector, matrix, or multidimensional array. Values can be any data type, and relate to the property specified by behavior.

Example: 'Joe'

Example: [1 2 3;4 5 6]

Number of times the property was set, specified as an integer.

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

Example: 5

Properties

expand all

Property values, specified as a scalar, vector, matrix, or multidimensional array. Values can be any data type, and relate to the property specified by behavior.

Property set 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

expand all

Create a mock for a person class.

testCase = matlab.mock.TestCase.forInteractiveUse;
[fakePerson,behavior] = testCase.createMock('AddedProperties',["Name" "Age"]);

Use the mock.

fakePerson.Name = 'David';

Construct passing cases.

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

Construct failing cases.

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

---------------------
Framework Diagnostic:
---------------------
Negated WasSet failed.
--> Property 'Name' was unexpectedly set to the specified value 1 time(s).
--> Observed property set(s) to any value:
        <Mock>.Name = 'David'

Specified property set:
    PropertySetBehavior
        <Mock>.Name = <IsAnything constraint>
testCase.verifyThat(behavior.Age,WasSet)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasSet failed.
--> Property 'Age' was never set.

Specified property set:
    PropertySetBehavior
        <Mock>.Age = <IsAnything constraint>
testCase.verifyThat(behavior.Name,WasSet('ToValue','Andy'))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasSet failed.
--> Property 'Name' was not set to the specified value.
--> Observed property set(s) to any value:
        <Mock>.Name = 'David'

Specified property set:
    PropertySetBehavior
        <Mock>.Name = 'Andy'
testCase.verifyThat(behavior.Name,WasSet('WithCount',5))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
WasSet failed.
--> Property 'Name' was not set to the specified value the expected number of times.
    
    Actual property set count:
             1
    Expected property set count:
             5
--> Observed property set(s) to any value:
        <Mock>.Name = 'David'

Specified property set:
    PropertySetBehavior
        <Mock>.Name = <IsAnything constraint>

Introduced in R2017a

Was this topic helpful?