Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Foundations Testing MiniTest

Antoine Boillot
Antoine Boillot
10,466 Points

Why is the assert_match method considered as 2 assertions ?

Hi all,

I was running some tests in Ruby and I noticed that the snippet of code below is considered as 2 assertions :

def test_assert_match
    assert_match /world/, 'hello world'
end

How come ?

Thanks a lot!

1 Answer

David O' Rojo
David O' Rojo
11,051 Points

It is because in its implementation, assert_match uses two calls to assert: one to check that the given matcher (regepx) responds to =~, and one to check that the matcher and the string do match.

def assert_match matcher, obj, msg = nil
  msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" }
  assert_respond_to matcher, :"=~"
  matcher = Regexp.new Regexp.escape matcher if String === matcher
  assert matcher =~ obj, msg
end

Thanks! I wondered about this, too!