Is it possible to create a function handle to a class property set method?

조회 수: 2 (최근 30일)
Probably a stupid question and I've searched the web and Matlab help for an hour with different search terms without luck so either this is so stupid no-one would ever consider doing it or I didn't get the right search terms!
I am writing a unit test for a class and I have a property set method which uses validateattributes to check what is being set.
I want a unit test to verify that an exception is thrown when I try to set invalid data. I have tried a load of variations around the following theme, this possibly not being the most intelligent...
testCase.assertError( @()testCase.traceDemo.set.traceData( myData ),?MException );
I can test for a more specific exception, but that isn't the problem other than that the test passes because it throws a syntax exception. My problem is getting the function handle to call the set property.
Is this possible?

채택된 답변

Andy Campbell
Andy Campbell 2013년 8월 5일
편집: Andy Campbell 2013년 8월 5일
Hi Adam,
To answer your direct question a set method is not something that you can create an anonymous function to. However, you can test the setter by simply adding one layer to your function handle. I find that for the purposes of testing setter methods the simplest and cleanest approach is just using nested functions likes so:
classdef SetterTest < matlab.unittest.TestCase
methods(Test)
function testInvalidInput(testCase)
objUnderTest = TestedObject;
testCase.verifyError(@setScalarPropertyToMatrix, ?MException);
function setScalarPropertyToMatrix
objUnderTest.PropertyShouldBeScalar = eye(5);
end
end
end
end
Hope that helps!
Andy

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by