Main Content

matlab.mock.constraints.WasSet Class

R2026b

Namespace: matlab.mock.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Test if mock object property was set

Description

The matlab.mock.constraints.WasSet class provides a constraint to test if a mock object property was set. To test whether an actual value satisfies the constraint, use the constraint with the assertThat, assumeThat, fatalAssertThat, or verifyThat method of the matlab.mock.TestCase class.

Because the matlab.mock.constraints.WasSet class is a subclass of the matlab.unittest.constraints.BooleanConstraint class, you can create constraints that can be combined and negated using the and (&), or (|), and not (~) operators.

Creation

Description

c = matlab.mock.constraints.WasSet creates a constraint to test if a mock object property was set. The constraint is satisfied by a matlab.mock.PropertyBehavior instance whose corresponding mock object property was set at least once.

example

c = matlab.mock.constraints.WasSet(Name=Value) specifies options using one or more name-value arguments. For example, c = matlab.mock.constraints.WasSet(ToValue=42,WithCount=3) creates a constraint that is satisfied if a property is set to 42 exactly 3 times.

example

Name-Value Arguments

expand all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: c = matlab.mock.constraints.WasSet(ToValue=42,WithCount=3)

Property value, specified as a value of any data type.

This argument sets the Value property.

Example: ToValue="David"

Example: ToValue=[1 2 3; 4 5 6]

Number of property set interactions, specified as a positive integer scalar.

This argument sets the Count property.

Example: WithCount=3

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Properties

expand all

Property value, specified as a value of any data type. The constraint includes this property when you create the constraint using the ToValue name-value argument.

Attributes:

GetAccess
public
SetAccess
immutable

Number of property set interactions, specified as a positive integer scalar. The constraint includes this property when you create the constraint using the WithCount name-value argument.

Attributes:

GetAccess
public
SetAccess
immutable

Examples

collapse all

Test for property set interactions by using the WasSet constraint.

First, import the classes used in this example.

import matlab.mock.TestCase
import matlab.mock.constraints.WasSet

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Create a mock with two properties.

[mock,behavior] = testCase.createMock(AddedProperties=["Name" "Age"]);

Use the mock to set the Name property.

mock.Name = "David";

Using the WasSet constraint, verify that Name was set at least once.

testCase.verifyThat(behavior.Name,WasSet)
Verification passed.

Test if the Age property was set. The test fails.

testCase.verifyThat(behavior.Age,WasSet)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    WasSet failed.
    --> Property 'Age' was never set.
    
    Specified property set operation:
    PropertySetBehavior
        <Mock>.Age = <IsAnything constraint>

Set the Name property to a new value. Then, verify that the property was set to that value.

mock.Name = "Erica";
testCase.verifyThat(behavior.Name,WasSet(ToValue="Erica"))
Verification passed.

Verify that the Name property was set twice.

testCase.verifyThat(behavior.Name,WasSet(WithCount=2))
Verification passed.

Test if the Name property was set to a specific value twice. The test fails.

testCase.verifyThat(behavior.Name,WasSet(ToValue="David",WithCount=2))
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:
             2
    --> All observed property set operation(s) where the property was set to any value are:
            <Mock>.Name = "David"
            <Mock>.Name = "Erica"
    
    Specified property set operation:
    PropertySetBehavior
        <Mock>.Name = "David"

Verify that the Name property was not set exactly once. The test passes because the property was set twice.

testCase.verifyThat(behavior.Name,~WasSet(WithCount=1))
Verification passed.

Version History

Introduced in R2017a