Hi,
I have been working with mockalate and trying to find a way to unit test my login code. The problem I am running into is that although the code works when I run the app, the unit tests are failing for some unknown reason.
There are a few parts to the actual test and they are:
Swiz: Calls a delegate function and returns an AsyncToken
Mocolate: Mocks the delegate function and returns an ASyncToken ResultResponse.
From what I have been able to troubleshoot, the app code itself handles the mock response correctly and assigns the login vales in the code under test, but as soon as I get back into the unitTest code, the values are blank. I see that there is a scopechain where all the values are, but they are cleared in the unit. Inside the code being tested, the mock performs as required and the model.username is set, but outside the app code, the value is blank? Basically, I have hit a brick wall in trying to mock this. Could someone help out?
Thanks
Des
Code for reference:
===============
// Deal with a login request
[Mediate(event="AuthenticationEvent.LOGIN")]
public function login(authenticationEvent:AuthenticationEvent):void {
// Handle successful login.
var loginResultEvent:Function = function(event:ResultEvent ):void {
// Write the username to the model for convenience
loginModel.username = authenticationEvent.username;
eventDispatcher.dispatchEvent(new AuthenticationEvent(AuthenticationEvent.LOGGEDIN));
}
// Handle login failure.
var loginFaultEvent:Function = function(faultEvent:FaultEvent):void {
eventDispatcher.dispatchEvent(new AuthenticationEvent(AuthenticationEvent.LOGIN_FAIL));
}
// Log into the Remote server channelset login
serviceHelper.executeServiceCall(loginDelegate.login(authenticationEvent.username,authent icationEvent.password),
loginResultEvent, loginFaultEvent);
}
This is called by the UnitTests
=======================
[Before]
public function runBeforeEveryTest():void
{
fixture = new LoginController();
// Mock up the delegate
fixture.loginDelegate = loginDelegateMock;
fixture.loginModel = new LoginModel();
fixture.eventDispatcher = new EventDispatcher();
}
[Test(async)]
public function loginTest_with_valid_user():void
{
// Create the authenticationEvent the Presenter would have sent
authenticationEvent = new AuthenticationEvent(AuthenticationEvent.LOGIN);
authenticationEvent.username = testUsername;
authenticationEvent.password = testPassword;
var result:Object = testURLCode;
var token:AsyncToken = new AsyncToken();
var resultEvent:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, false, result, token);
// Mock the response
mock(loginDelegateMock).method("login").args(authenticationEvent.username, authenticationEvent.password).returns(token).answers(new ResultAnswer(token, resultEvent));
//execute the method
fixture.login(authenticationEvent);
Assert.assertEquals("Username is in Model", testUsername, fixture.loginModel.username);
}
Trace from run
===========
Username is in Model - expected:<a@b.com> but was:<>
at flexunit.framework::Assert$/failWithUserMessage(Assert.as:591)
at flexunit.framework::Assert$/failNotEquals(Assert.as:394)
at flexunit.framework::Assert$/assertEquals(Assert.as:98)
**[ at com.wl.sfx.unit.controller::LoginControllerTests/loginTest_with_valid_user(LoginControlle rTests.as:141) ]
at Function/http://adobe.com/AS3/2006/builtin::apply
at flex.lang.reflect::Method/apply(Method.as:208)
at ReflectiveCallable/run(FrameworkMethod.as:297)
at org.flexunit.runners.model::FrameworkMethod/applyExplosivelyAsync(FrameworkMethod.as:171)
at org.flexunit.runners.model::FrameworkMethod/invokeExplosivelyAsync(FrameworkMethod.as:186 )
at org.flexunit.internals.runners.statements::InvokeMethod/evaluate(InvokeMethod.as:77)
at org.flexunit.internals.runners.statements::ExpectAsync/evaluate(ExpectAsync.as:544)
at org.flexunit.internals.runners.statements::StackAndFrameManagement/handleTimerComplete(St ackAndFrameManagement.as:141)
at flash.events::EventDispatcher/dispatchEventFunction
at flash.events::EventDispatcher/dispatchEvent
at flash.utils::Timer/tick