我正在尝试为flex中的视图编写单元测试。 TextInput reportName
和属性_parameters
之间存在双向绑定。我运行代码时绑定工作正常,但测试失败。以下是许多以类似方式失败的测试之一。测试失败,并显示以下消息:
Error: Expected: "newName"
but: was "defaultTitle"
parameters_changeHandler()
中的跟踪消息显示以下内容:
parameters_changeHandler
_parameters.reportName defaultTitle
reportName.text newName
所以TextInput,reportName
正在正确更新,但我在测试中设置文本的方式是撤消绑定,至少我认为这是问题。
如何维护双重绑定并仍然更新值?有没有其他方法可以模拟用户输入数据?
View.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>
LocationAnalysisParameters.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);
}
}
}
答案 0 :(得分:0)
我想我只需要睡觉就可以了。我的测试失败的原因是双向绑定仅当你将它们指定为双向绑定时才用作双向绑定。
错误(单向绑定):
text="{_parameters.reportName}"
正确(双向绑定:
text="@{_parameters.reportName}"
正确指定两个绑定是使事情有效所需的全部内容。