0

我正在尝试为 flex 中的视图编写单元测试。reportNameTextInput和属性之间有两种方式绑定_parameters。当我运行代码时绑定工作正常,但测试失败。以下是许多以类似方式失败的测试之一。测试失败并显示以下消息:

Error: Expected: "newName"
but: was "defaultTitle"

中的跟踪消息parameters_changeHandler()显示以下内容:

parameters_changeHandler
_parameters.reportName defaultTitle
reportName.text newName

所以 TextInputreportName正在正确更新,但是我在测试中设置文本的方式是撤消绑定,至少我认为这是问题所在。

如何维护两个绑定并仍然更新值?是否有另一种方法来模拟用户输入数据?

查看.mxml:

    <fx:Script>
        <![CDATA[
            import events.events.UpdatePointRadiusReportModalParametersEvent;
            import model.LocationAnalysisParameters;

            [Bindable]
            internal var _parameters:LocationAnalysisParameters;

            public function set parameters(parameters:LocationAnalysisParameters):void
            {
                this._parameters = parameters;
            }

            public function get parameters():LocationAnalysisParameters
            {
                return _parameters;
            }

            protected function parameters_changeHandler():void
            {
                trace('parameters_changeHandler');
                trace('_parameters.reportName', _parameters.reportName);
                trace('reportName.text', reportName.text);
                dispatchEvent(new UpdatePointRadiusReportModalParametersEvent(_parameters));
            }
        ]]>
    </fx:Script>

    <s:VGroup>
        <s:Label id="reportNameLabel"
            styleName="toolStyle"
            text="Report Name:"/>
        <s:TextInput id="reportName"
            width="100%"
            maxChars="50"
            minWidth="200"
            styleName="toolStyle"
            text="{_parameters.reportName}"
            change="parameters_changeHandler()"
            restrict="a-zA-Z0-9 \-_"/>
    </s:VGroup>

</components:FlyoutPanel>

位置分析参数.as

package model
{
    import utils.StringUtils;

    [Bindable]
    public class LocationAnalysisParameters
    {
        public var reportName:String;

        public var reportDescription:String;

        public function reset():void
        {
            reportName = null;
            reportDescription = null;
        }

        public function isValid():Boolean
        {
            return !StringUtils.isNullOrEmpty(reportDescription) && !StringUtils.isNullOrEmpty(reportName);
        }

        public function clone():LocationAnalysisParameters
        {
            const c:LocationAnalysisParameters = new LocationAnalysisParameters();
            c.reportName = reportName;
            c.reportDescription = reportDescription;
            return c;
        }
    }
}

ViewTest.as 包视图 {

    public class PointRadiusReportPopupViewTest
    {

        private var popupView:PointRadiusReportPopupView;


        [Before(async, ui)]
        public function setUp():void
        {
            popupView = new PointRadiusReportPopupView();
            Async.proceedOnEvent(this, popupView, FlexEvent.CREATION_COMPLETE);
            UIImpersonator.addChild(popupView);
        }

        [After(ui)]
        public function tearDown():void
        {
            UIImpersonator.removeChild(popupView);
            popupView = null;
        }

        [Test(async, ui)]
        public function reportNameChangeShouldDispatchUpdatePointRadiusReportModalParametersEvent():void
        {
            //arrange
            const parameters:LocationAnalysisParameters = new LocationAnalysisParameters();
            parameters.reportName = 'defaultTitle';
            parameters.reportDescription = 'defaultDescription';
            popupView.parameters = parameters;
            const expectedParameters:LocationAnalysisParameters = new LocationAnalysisParameters();
            expectedParameters.reportName = 'newName';
            expectedParameters.reportDescription = 'defaultDescription';

            //act
            Async.handleEvent(this, popupView, UpdatePointRadiusReportModalParametersEvent.TYPE, assertUpdatePointRadiusReportModalParametersEvent, 500, expectedParameters);
            updateTextInput(popupView.reportName, 'newName');
        }

        private function assertUpdatePointRadiusReportModalParametersEvent(evt:UpdatePointRadiusReportModalParametersEvent, expectedParameters:LocationAnalysisParameters):void
        {
            assertThat(evt.parameters.reportName, equalTo(expectedParameters.reportName));
            assertThat(evt.parameters.reportDescription, equalTo(expectedParameters.reportDescription));
        }

        /**
         * Helpers
         */

        internal static function updateTextInput(compontent:TextInput, value:String):void
        {
            compontent.text = value;
            const event:TextOperationEvent = new TextOperationEvent(TextOperationEvent.CHANGE, false, true);
            compontent.dispatchEvent(event);
        }
    }
}
4

1 回答 1

0

我想我只需要睡在上面。我的测试失败的原因是,当您将它们指定为双向绑定时,双向绑定仅作为双向绑定起作用。

错误(单向绑定):

 text="{_parameters.reportName}"

正确(双向绑定:

 text="@{_parameters.reportName}"

正确指定这两个绑定是使事情正常工作所需要的。

于 2013-10-15T17:20:35.147 回答