Unit testing Example using Busted, based on the HelloWorld

Coming from a strong TDD background I was upset when I found that there wasn’t a lot of examples of unit testing the Lua part of Corona so I threw a new one together, using spies and mocks. It’s not a in depth example but Spy’s and mocks were the areas I found missing in other examples I’ve viewed.

For this example I abstracted the Change Text Color Button into its own class and added unit tests for it using Busted. I also mocked the display class and my button because we don’t have access to them in a pure LUA environment.

Please check it out,

https://github.com/chris-allnutt/unit-tested-corona [import]uid: 130806 topic_id: 34533 reply_id: 334533[/import]

Also I’d be happy to add more examples if people can tell me what they’re looking for, [import]uid: 130806 topic_id: 34533 reply_id: 137309[/import]

Also I’d be happy to add more examples if people can tell me what they’re looking for, [import]uid: 130806 topic_id: 34533 reply_id: 137309[/import]

Chris - thanks a ton for your code on getting a test setup with Busted. One area I think Corona could improve is including better support for tests. I was able to setup some basic tests, but I hit a wall trying to figure out how to test frame based events. 

Currently I’ve got something like:

Runtime:addEventListener("enterFrame", update)

And in my update function, I do a bunch of things like increment timers.

I wonder what the best way to mock this out would be ?

Chris - thanks a ton for your code on getting a test setup with Busted. One area I think Corona could improve is including better support for tests. I was able to setup some basic tests, but I hit a wall trying to figure out how to test frame based events. 

Currently I’ve got something like:

Runtime:addEventListener("enterFrame", update)

And in my update function, I do a bunch of things like increment timers.

I wonder what the best way to mock this out would be ?

I think the difficulty you’re going to run into is that theres no way to really mock an “enterFrame” event.  

The approach I would take is create a mock for Runtime, the confirm that addEventListener was called with “enterFrame”, update.

Then write a test for update on its own.

If you want to get more advanced you could mock out a Runtime object with addEventListener keeping a table/array of all registered objects, then using a method like “fireEvent” on the same mock object with “enterFrame” as the event, but thats a fuzzy area vs strict unit testing.

Hope this helps and I’m sorry It took me so long to respond, been hugely busy with getting my app ready to launch.

I think with the timers you should verify they are each called with their appropriate methods, and replace any closures you have with calls to actual methods.  Then you can test those individual methods on their own, and confirm that the timers are called with proper parameters.

On a side note with my next app I’m going to attempt making a test app setup so I can actually run my tests in the simulator, but I’m thinking that might break horribly so don’t get your hopes up.

I think the difficulty you’re going to run into is that theres no way to really mock an “enterFrame” event.  

The approach I would take is create a mock for Runtime, the confirm that addEventListener was called with “enterFrame”, update.

Then write a test for update on its own.

If you want to get more advanced you could mock out a Runtime object with addEventListener keeping a table/array of all registered objects, then using a method like “fireEvent” on the same mock object with “enterFrame” as the event, but thats a fuzzy area vs strict unit testing.

Hope this helps and I’m sorry It took me so long to respond, been hugely busy with getting my app ready to launch.

I think with the timers you should verify they are each called with their appropriate methods, and replace any closures you have with calls to actual methods.  Then you can test those individual methods on their own, and confirm that the timers are called with proper parameters.

On a side note with my next app I’m going to attempt making a test app setup so I can actually run my tests in the simulator, but I’m thinking that might break horribly so don’t get your hopes up.

Good article, please write more example, i very need it. Example about script unit test on corona??

Thanks  chris-allnutt

Good article, please write more example, i very need it. Example about script unit test on corona??

Thanks  chris-allnutt

Hello!

I really like busted but I see no examples with a context. I opened an issue and may contribute to the project when I have time.

Hello!

I really like busted but I see no examples with a context. I opened an issue and may contribute to the project when I have time.

This article and repo are great.

This article and repo are great.