# Scope problem with signals and MiniTest

**URL:** <https://www.ruby-forum.com/t/scope-problem-with-signals-and-minitest/214636>\
**Category:** Ruby-Gnome 2\
**Created:** [December 7, 2011, 11:22pm UTC](https://www.ruby-forum.com/t/scope-problem-with-signals-and-minitest/214636 "2011-12-07T23:22:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [December 7, 2011, 11:22pm UTC](https://www.ruby-forum.com/t/scope-problem-with-signals-and-minitest/214636/1 "2011-12-07T23:22:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [December 8, 2011, 1:54am UTC](https://www.ruby-forum.com/t/scope-problem-with-signals-and-minitest/214636/2 "2011-12-08T01:54:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [December 8, 2011, 2:06am UTC](https://www.ruby-forum.com/t/scope-problem-with-signals-and-minitest/214636/3 "2011-12-08T02:06:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [December 10, 2011, 2:24pm UTC](https://www.ruby-forum.com/t/scope-problem-with-signals-and-minitest/214636/4 "2011-12-10T14:24:10Z")

</div>

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