Introduction
I am learning myself Ruby not because I need to but just because I can. In my first post I showed you my setup that I will be using for now. And I got hello world working.
Now I want to write a test to see that my output is what it is supposed to be.
The problems
In encountered a few problems when trying to make my test work. First was how to capture the stdoutput. This was fairly easy, namely Google. I found this thinkingdigitaly post very useful.
And I used this little piece of code to make it work for me.
```ruby require ‘stringio’
module Kernel
def capture_stdout out = StringIO.new $stdout = out yield return out ensure $stdout = STDOUT end
end``` Which made me look for a way to include this file in my tests because I wanted to keep this separate from my testcode.
It seems that you need to use require_relative “nameoffile” to do that.
I then needed a function in my helloworld code so I could invoke it and test it.
So this became.
ruby
#!/usr/bin/ruby
def helloworld
puts "Hello world advanced"
end
def seems to be the way to go.
The test
And now it is time to see test. I used TestUnit for this and not rspec.
```ruby require “test/unit” require_relative “module” require_relative “helloworld”
class MyTest < Test::Unit::TestCase
def test_if_hello_word_advanced_is_put_on_stdout
out = capture_stdout do
helloworld
end
assert_equal "Hello world advancedn", out.string
end end``` Tests just begin with the word test and the class inherits from a bunch of stuff. and the assert_equal seems to be obvious.
When I run the above I get this.
I have to figure out this gems thing pretty soon me thinks. And then get rubymine to play nice with the test framework.
Conclusion
Google to the rescue. But in the end it went pretty smoothly. At least I got my first test running. Test are very important in any language so they are the best place to start learning.