필터 지우기
필터 지우기

How to use IgnoringFields in StructComparator?

조회 수: 1 (최근 30일)
Lena
Lena 2024년 5월 16일
댓글: Lena 2024년 5월 16일
In my unit test I am comparing two structs s1 and s2 both with the same field names but different field values. How do I ignore a set of fields based on the field names? I found Comparator for structure arrays - MATLAB - MathWorks Benelux but no examples for IgnoringFields are shown.
I tried something like this:
testCase.verifyThat(s1,IsEqualTo(s1,"Using",StructComparator(NumericComparator,"IgnoringFields",["field7","field10"])))
The error says:
Error using matlab.unittest.constraints.Comparator/throwUnsupportedValue
None of the currently available comparators support the value.
Available Comparators:
--> matlab.unittest.constraints.NumericComparator
Value (cell):
1×1 cell array
{[1.23456789e+10]}

채택된 답변

Steven Lord
Steven Lord 2024년 5월 16일
Rather than building a StructComparator, just specify "IgnoringFields" in your IsEqualTo call.
s1 = struct('a', 1, 'b', 2);
s2 = struct('a', 1, 'b', 3);
import matlab.unittest.constraints.IsEqualTo
testcase = matlab.unittest.TestCase.forInteractiveUse;
verifyThat(testcase, s1, IsEqualTo(s2, "IgnoringFields", "b"))
Verification passed.

추가 답변 (0개)

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by