Learn how to write simple tests

Hello,

Im now trying to do the first exercises of codewars.
The exercises are not a problem.

As I example I use the hello world programm.

I can write

def greeter
puts “Hello World!”
end

But how on earth can I make a test-case for this using for example
assert ?

Roelof

On Mon, May 26, 2014 at 10:44:13PM +0200, Roelof W. wrote:

But how on earth can I make a test-case for this using for example assert ?

You can test that the greeter method returns hello world, on ruby
1.9.3+ like this (run ruby greeter_test.rb):

# greeter.rb
def greeter
  "Hello World!"
end

# greeter_test.rb
require "minitest/autorun"
require_relative "greeter"

class SimpleTest < Minitest::Unit::TestCase
  def test_greeter
    assert_equal greeter, "Hello World!"
  end
end

Testing that it calls puts with the “Hello World!” args is more
difficult.

On Monday, May 26, 2014 10:44:13 PM Roelof W. wrote:

puts "Hello World!"

end

But how on earth can I make a test-case for this using for example assert ?

Roelof

It is easy with -
http://www.ruby-doc.org/stdlib-2.1.1/libdoc/test/unit/rdoc/Test/Unit/Assertions.html#method-i-assert_equal

assert_equal “Hello World!”, greeter

Amadeus F. schreef op 26-5-2014 22:59:

 # greeter_test.rb

difficult.

hmm ,
When I do this solution I see this eror :
NoMethodError: undefined method `require’ for main:Object

when I do the assert_equal line alone I see this error :
NoMethodError: undefined method `assert_equal’ for main:Object

Roelof

Which ruby version are you using?

On 2.1.1 it runs fine.

In Rspec you can describe it like this:

<<FILE
require ‘rspec’

def hello
puts “hello world”
end

describe “hello” do
it “should print to $stdout ‘hello world\n’” do
@stdout_orig, $stdout = $stdout, StringIO.new
hello
expect($stdout.string).to eq(“hello world\n”)
end
end
FILE

Run the example spec with rspec FILE

Mistral CONTRASTIN schreef op 26-5-2014 23:40:

If you are doing it in codewars the chances are they undefined require. The
testing advice here is given for normal execution.

Mistral

On 26 May 2014, at 22:20, Roelof W. [email protected] wrote:

Im working on codewars. I will try to ask them for some help.

Roelof

If you are doing it in codewars the chances are they undefined require.
The testing advice here is given for normal execution.

Mistral

Mistral CONTRASTIN schreef op 26-5-2014 23:40:

If you are doing it in codewars the chances are they undefined require. The
testing advice here is given for normal execution.

Mistral

On 26 May 2014, at 22:20, Roelof W. [email protected] wrote:

Thanks,

Can anyone help me figure out why I see here a syntax error :

class Testgreeter > Test:Unit:TestCase

 def test
    assert_equal("Hello World!", "output is not Hello World")
 end

end

Roelof

On 27 Μαϊ 2014, at 09:31 , Roelof W. [email protected] wrote:

Can anyone help me figure out why I see here a syntax error :

class Testgreeter > Test:Unit:TestCase

Try: class Testgreeter < Test:Unit:TestCase

def test
assert_equal(“Hello World!”, “output is not Hello World”)
end
end

Roelof

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

“As you set out for Ithaca, hope the voyage is a long one, full of
adventure, full of discovery […]” - C. P. Cavafy

Panagiotis A. schreef op 27-5-2014 9:41:

Thanks,

Roelof

Changed it and still the syntax error message.

Roelof

Oh…
“:” -> “::”

Try this code:
class Testgreeter < Test::Unit::TestCase
end

2014-05-27 11:46 GMT+04:00 Roelof W. [email protected]:

??? ??? schreef op 27-5-2014 10:33:

Oh…
“:” -> “::”

Try this code:
class Testgreeter < Test::Unit::TestCase
end

found it after a little bit trying.

the right answer is :

but now I have this :

def no_odds( values )
values.select { |x| x.even?}
end

and these test cases :

Test.assert_equals([2] , no_odds([2]))
Test.assert_raise(NoMethodError , no_odds([“a”]), “Er bestaat geen ?even
voor een string”)

Still I see this error message :

NoMethodError: undefined method `even?’ for “a”:String

NoMethodError: undefined method `even?’ for “a”:String

How can I take care that my error message is shown or even that there is
no error message too seen as I run the tests.

Roelof

On Tuesday 27 May 2014 11:50:15, Roelof W. [email protected] wrote:

How can I take care that my error message is shown or even that there is
no error message too seen as I run the tests.

I think you confuse the reason for a unit test with a syntactic test.
The unit
test asserts that your syntactic correct code produces the desired
results.

Basically, your code:

Test.assert_raise(NoMethodError , no_odds([“a”]))

Says this: “I don’t trust the Ruby Interpreter, so I want to write a
test
where I check whether the interpreter finds out that there’s no #even?
for a
string or not”. This is not what unit testing is supposed to do.

If you had extra safeguards in your code, you could check them. For
example,
consider this:

—%<—
def no_odds(values)
return [] unless values.class == Array
values.select { |x| x.respond_to? :even? && x.even?}
end
—>%—

Now you could test whether your code works right:

—%<—
assert_equal [], no_odds(2)
assert_equal [2], no_odds([1, 2, “a”, “c”, 3])
—>%—

HTH

  --- Eric

I don’t know ‘even?’ method.
Look here about all methods of String class:
http://ruby-doc.org/core-2.1.0/String.html

2014-05-27 13:50 GMT+04:00 Roelof W. [email protected]:

Excuse the typo:

On Tuesday 27 May 2014 15:11:54, Eric MSP Veith
[email protected]
wrote:

—%<—
def no_odds(values)
return [] unless values.class == Array
values.select { |x| x.respond_to? :even? && x.even?}
end
—>%—

Should be:

—%<—
def no_odds(values)
return [] unless values.class == Array
values.select {|x| x.respond_to?(:even?) && x.even? }
end
—>%—

Note the parentheses.

--- Eric

Curious. How do you test a function embedded in another function?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs