Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Extending Gosu / Gosu-mock, a tool for unit testing with Gosu
- - By philomory Date 2010-05-13 02:04 Edited 2010-05-13 02:07
I finally decided I needed some way to unit test my Gosu code. So, I started building Gosu::Mock.

adam-gardners-imac-g5:~/code/other projects/gosu-mock adam$ cat example.rb
require 'test/unit'
require 'gosu_mock'

class GosuMockExample < Test::Unit::TestCase
  class MockWindow < Gosu::Window
    def initialize
      @image =,"picture.png",false)
    def draw
      @image.draw(0,0,0) unless button_down?(5) # I haven't added the constants yet, but they'll come
  def test_window_draws_image
    window =
    assert_equal "picture.png", window.drawing_events[0].content.filename
  def test_window_doesnt_draw_image_when_button_is_pressed
    window =
    assert window.drawing_events.empty?

adam-gardners-imac-g5:~/code/other projects/gosu-mock adam$ ruby example.rb
Loaded suite example
Finished in 0.003881 seconds.

2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
adam-gardners-imac-g5:~/code/other projects/gosu-mock adam$

Note that  Gosu-Mock steps in for Gosu; once you require it, the class your testing just inherits from Gosu::Window like normal, no need for a special 'testing version' of your class.

Gosu mock is *far* from feature complete at this point, but, I only started writing it this morning. You can get the source here, or see a more practical use of the library in my CuteDemos project.
Parent - By ippa Date 2010-06-29 08:51
Hey, is this project still alive?

Starting a test-suite for Chingu and this looks useful :). When do we get a gem?

Also in do_tick() - Not sure if it matters, but shouldn't draw be called before update, since it's how Gosu does it?
Up Topic Gosu / Extending Gosu / Gosu-mock, a tool for unit testing with Gosu

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill