如何在ActionScript 3中伪造AsyncToken返回值

时间:2010-05-07 10:17:23

标签: actionscript-3 mocking parsley asynctoken

使用欧芹, 我有一个通过a访问的服务 [命令(选择= '列表')] public function getRssFeed(msg:RssEvent):AsyncToken { 将service.list()作为AsyncToken返回; }

当我指向“Real”RssService时,一切都按预期工作。我的问题是当我指向“模拟”RssService时。我无法弄清楚如何使用一些虚拟数据返回来伪造AsyncToken ...有谁知道如何做到这一点?

4 个答案:

答案 0 :(得分:4)

已解决..............;)

 public function list():AsyncToken

     var rssFeed:Array = [rss,rss,rss];
     var token:AsyncToken = createToken(rssFeed);
     token.addResponder(new AsyncResponder(resultHandler, null));
     return token;

  }

  private function resultHandler(event:ResultEvent, token:AsyncToken = null):void
  {
     event.token.dispatchEvent(event);   
  }


  protected function createToken(result:Object):AsyncToken
  {
     var token:AsyncToken = new AsyncToken(null);
     setTimeout(applyResult, Math.random()*500, token, result);
     return token;
  }

  private function applyResult(token:AsyncToken, result:Object):void
  {
     mx_internal:token.setResult(result);
     var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, result, token);
     mx_internal:token.applyResult(event);
     trace(token);
  }

答案 1 :(得分:3)

使用Parsley 3.0,您可以使用spicefactory异步命令获得更好的选择:

public class MockCommand
{
    public var callback:Function;

    public function execute():void
    {
        var timer:Timer = new Timer(500, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_completeHandler);
        timer.start();
    }

    private function timer_completeHandler(event:TimerEvent):void
    {
        callback(mockResultData);
    }
}

无需mx_internal导入。

答案 2 :(得分:1)

不要忘记添加:

use namespace mx_internal;

否则你会得到这个例外。

[Fault] exception,information = TypeError:错误#1006:setResult不是函数。

答案 3 :(得分:1)

我使用某种ServiceProxy模式,它基本上提供了3种方法:

  • invokeOperation
  • mockResultInvokeOperation
  • mockFaultInvokeOperation

我以下列方式扩展ServiceProxy:

public class SomeService extends ServiceProxy {
    public var useMock:Boolean;

    public function someServiceCall(arg1:Type, arg2:Type, responder:IResponder):AsyncToken {
        if (useMock) {
            mockResultInvokeOperation({some fake result object}, responder);
        }

        return invokeOperation("someServiceCall", [arg1, arg2], responder);
    }

}

技术方面我使用与你完全相同的技巧。

package com.obecto.services.proxy
{
import flash.utils.setTimeout;

import mx.core.mx_internal;
import mx.messaging.messages.RemotingMessage;
import mx.rpc.AbstractOperation;
import mx.rpc.AbstractService;
import mx.rpc.AsyncToken;
import mx.rpc.Fault;
import mx.rpc.IResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public class ServiceProxy
{
    public var service:AbstractService;

    public function invokeOperation(operationName:String, arguments:Array, responder:IResponder):AsyncToken 
    {
        var operation:AbstractOperation = service.getOperation(operationName);
        operation.arguments = arguments;

        var token:AsyncToken = operation.send();
        token.addResponder(responder);
        return token;
    }

    public function mockResultInvokeOperation(mockResult:Object, responder:IResponder):AsyncToken
    {
        var fakeMessage:RemotingMessage = new RemotingMessage();

        var token:AsyncToken = new AsyncToken(fakeMessage);
        token.addResponder(responder);

        setTimeout(
            function(e:ResultEvent = null):void 
            {
                token.mx_internal::applyResult(new ResultEvent(ResultEvent.RESULT, false, true, mockResult));
            }, 1000);

        return token;
    }

    public function mockFaultInvokeOperation(message:String, responder:IResponder):AsyncToken
    {
        var fakeMessage:RemotingMessage = new RemotingMessage();

        var token:AsyncToken = new AsyncToken(fakeMessage);
        token.addResponder(responder);

        setTimeout(
            function(e:ResultEvent = null):void 
            {
                token.mx_internal::applyFault(new FaultEvent(FaultEvent.FAULT, false, true, 
                    new Fault("MOCK_FAULT", message)));
            }, 1000);

        return token;
    }

}
}