drewbourne / mockolate

fake chocolate, mock objects and test spies for AS3
http://mockolate.org/
MIT License
145 stars 27 forks source link

new Runner syntax #1

Closed ghost closed 14 years ago

ghost commented 14 years ago

Drew

Thought I'd try this approach to log our conversations. I tried the new notation you presented around the runner, but I must be doing something wrong?

Original code that works

import mockolate.*;
import mockolate.ingredients.answers.*;
import mockolate.prepare;

public class LoginPresentorTests
{   
    // SUT
    private var loginPresentor:LoginPresentor;
    // Mock for the Model within LoginPresentor
    private var loginModelMock:LoginModel;

    [Before(order=1, async, timeout=5000)]
    public function prepareMockolates():void
    {
        Async.proceedOnEvent(this, prepare(LoginModel), Event.COMPLETE);
    }

    [Before(order=2)]
    public function runBeforeEveryTest():void
    {
        loginPresentor = new LoginPresentor();
        // Event within the test and not on the LoginPresentor
    }

    [Test(async)]
    public function testLogin():void
    {
        // Mock the dependent classes
        loginModelMock = strict(LoginModel);
        // Stub
        // No current behavior to mock

        // Assign the mock to the LoginPresentor
        loginPresentor.loginModel = loginModelMock;

        Assert.assertNotNull("LoginModel Mock available", loginModelMock);

}

New code that fails

import mockolate.runner.MockolateRunner; MockolateRunner;

import org.flexunit.async.Async;

[RunWith("mockolate.runner.MockolateRunner")]
public class LoginPresentorTests
{   
    // SUT
    private var loginPresentor:LoginPresentor;

    [Mock(type="strict")]
    private var loginModelMock:LoginModel;

    [Before]
    public function runBeforeEveryTest():void
    {
        loginPresentor = new LoginPresentor();
    }

    [Test(async)]
    public function testLogin():void
    {
        // No current behaviour to mock

        // Assign the mock to the LoginPresentor
        loginPresentor.loginModel = loginModelMock;

        Assert.assertNotNull("LoginModel Mock available", loginModelMock);

}

Fails with assert not null. This is the fail stacktrace

LoginModel Mock available - object was null: null at flexunit.framework::Assert$/failWithUserMessage(Assert.as:591) at flexunit.framework::Assert$/failNull(Assert.as:533) at flexunit.framework::Assert$/assertNotNull(Assert.as:311) **[ at com.wl.sfx.unit.presentor::LoginPresentorTests/testLogin(LoginPresentorTests.as:81) ] 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/evaluate(StackAndFrameManagement.as:129) at org.flexunit.internals.runners.statements::StatementSequencer/executeStep(StatementSequencer.as:97) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at org.flexunit.internals.runners.statements::AsyncStatementBase/sendComplete(AsyncStatementBase.as:76) at org.flexunit.internals.runners.statements::StatementSequencer/sendComplete(StatementSequencer.as:165) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:138) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at org.flexunit.internals.runners.statements::InvokeMethod/handleMethodExecuteComplete(InvokeMethod.as:89) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at org.flexunit.runners.model::FrameworkMethod/applyExplosivelyAsync(FrameworkMethod.as:173) 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::SequencerWithDecoration/executeStep(SequencerWithDecoration.as:104) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.internals.runners.statements::StatementSequencer/evaluate(StatementSequencer.as:108) at org.flexunit.internals.runners.statements::StatementSequencer/executeStep(StatementSequencer.as:97) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at mockolate.runner::Inject/evaluate(Inject.as:35) at org.flexunit.internals.runners.statements::StatementSequencer/executeStep(StatementSequencer.as:97) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.internals.runners.statements::StatementSequencer/evaluate(StatementSequencer.as:108) at org.flexunit.runners::BlockFlexUnit4ClassRunner/runChild(BlockFlexUnit4ClassRunner.as:130) at org.flexunit.internals.runners::ChildRunnerSequencer/executeStep(ChildRunnerSequencer.as:82) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.internals.runners.statements::StatementSequencer/evaluate(StatementSequencer.as:108) at org.flexunit.internals.runners.statements::StatementSequencer/executeStep(StatementSequencer.as:97) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at org.flexunit.internals.runners.statements::AsyncStatementBase/sendComplete(AsyncStatementBase.as:76) at org.flexunit.internals.runners.statements::StatementSequencer/sendComplete(StatementSequencer.as:165) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:138) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at org.flexunit.internals.runners.statements::AsyncStatementBase/sendComplete(AsyncStatementBase.as:76) at org.flexunit.internals.runners.statements::StatementSequencer/sendComplete(StatementSequencer.as:165) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:138) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at org.flexunit.internals.runners.statements::InvokeMethod/handleMethodExecuteComplete(InvokeMethod.as:89) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at org.flexunit.runners.model::FrameworkMethod/applyExplosivelyAsync(FrameworkMethod.as:173) 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::SequencerWithDecoration/executeStep(SequencerWithDecoration.as:104) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.internals.runners.statements::StatementSequencer/evaluate(StatementSequencer.as:108) at org.flexunit.internals.runners.statements::StatementSequencer/executeStep(StatementSequencer.as:97) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.token::AsyncTestToken/sendResult(AsyncTestToken.as:118) at mockolate.runner::Prepare/evaluate(Prepare.as:31) at org.flexunit.internals.runners.statements::StatementSequencer/executeStep(StatementSequencer.as:97) at org.flexunit.internals.runners.statements::StatementSequencer/handleChildExecuteComplete(StatementSequencer.as:134) at org.flexunit.internals.runners.statements::StatementSequencer/evaluate(StatementSequencer.as:108)

Thanks Des

drewbourne commented 14 years ago

Instance variables marked with [Mock] must be public for the runner to see them:

[Mock(type="strict")]
private var loginModelMock:LoginModel;

wont work, change it to:

[Mock(type="strict")]
public var loginModelMock:LoginModel;

and you should be good to go.

ghost commented 14 years ago

Doh! That was it.

PS. What does this line do:

import mockolate.runner.MockolateRunner; MockolateRunner;

I have seen the import, but the extra MockolateRunner; after the import? If it is not there, the test fails with errors

Thanks Des

drewbourne commented 14 years ago

It is just a compact way of writing an import and referencing the class to ensure it gets compiled into the application.

Other ways of doing it include:

import mockolate.runner.MockolateRunner; 
MockolateRunner;

or

import mockolate.runner.MockolateRunner; 
public var refs:Array = [MockolateRunner];