Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,050 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!