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

2 ビュー (過去 30 日間)
Adam
Adam 2013 年 8 月 5 日
編集済み: per isakson 2017 年 10 月 2 日
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
  1 件のコメント
Adam
Adam 2013 年 8 月 6 日
Thanks Andy. That does the job very well and is still very neat.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by