Forum: Ruby-Gnome 2 Scope problem with signals and MiniTest

Posted by Phil Thompson (philthompson)
on 2011-12-07 23:22
require 'minitest/autorun'
require 'gtk2'

class TestWindow < MiniTest::Unit::TestCase
  def test_window_fires_event
    closing = false
    window = Gtk::Window.new
    window.signal_connect('delete_event') { puts 'closing'; closing =
true; false }
    window.signal_emit('delete_event',
Gdk::EventAny.new(Gdk::Event::DELETE))
    puts "HAS BEEN CLICKED: #{closing}"
    assert_equal true, closing
  end
end

#TestWindow.new.test_window_fires_event

I have a problem when trying to test that I've correctly connected a
signal by ensuring the block is fired when emitting it. I've simplified
the test a great deal. This is a functional test and as such I really
want the block to be executed.

If I run the code directly (uncomment the last line and comment out
MiniTest::Unit::TestCase and the assert then it runs fine. It fails,
however, when running it as a test. I guess it's got something to do
with the context it's run in as a test and how signal_connect stores the
block but I'm stretching my knowledge of blocks and GTK signals here.

I'd be grateful for any suggestions.

Thanks,
Phil
Posted by Phil Thompson (philthompson)
on 2011-12-08 01:54
This appears to be an issue with minitest. I've just tried the same test 
in RSpec and it works.

I'll post a MiniTest Github issue but if anyone could suggest a 
workaround I'd love to hear it.

Thanks.
Posted by Phil Thompson (philthompson)
on 2011-12-08 02:06
Why is it I spend so long on a problem and when I finally post it I then 
go and find the solution. Cardboard programmer syndrome I guess.

Anyway, I needed to require 'gtk2' before 'minitest/autorun'.
Posted by Marc Heiler (shevegen)
on 2011-12-10 14:24
> Anyway, I needed to require 'gtk2' before 'minitest/autorun'.

:D

But that is helpful and useful, others can find this post when
having problem with minitest + ruby-gtk.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.