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

2 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Andy Campbell
Andy Campbell il 5 Ago 2013
Modificato: Andy Campbell il 5 Ago 2013
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

Più risposte (0)

Categorie

Scopri di più su App Building in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by