Scope problem with signals and MiniTest

require ‘minitest/autorun’
require ‘gtk2’

class TestWindow < MiniTest::Unit::TestCase
def test_window_fires_event
closing = false
window =
window.signal_connect(‘delete_event’) { puts ‘closing’; closing =
true; false }
puts “HAS BEEN CLICKED: #{closing}”
assert_equal true, closing

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.


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.


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’.

Anyway, I needed to require ‘gtk2’ before ‘minitest/autorun’.


But that is helpful and useful, others can find this post when
having problem with minitest + ruby-gtk.