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

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 Mon, May 26, 2014 at 10:44:13PM +0200, Roelof Wobben wrote:

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

Amadeus Folego schreef op 26-5-2014 22:59:

···

On Mon, May 26, 2014 at 10:44:13PM +0200, Roelof Wobben 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.

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

It is easy with - Module: Test::Unit::Assertions (Ruby 2.1.1)

assert_equal "Hello World!", greeter

···

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

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

--

Regards,
Arup Rakshit

Which ruby version are you using?

On 2.1.1 it runs fine.

···

On Mon, May 26, 2014 at 11:20:14PM +0200, Roelof Wobben wrote:

Amadeus Folego schreef op 26-5-2014 22:59:
>On Mon, May 26, 2014 at 10:44:13PM +0200, Roelof Wobben 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.
>

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

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 Wobben <r.wobben@home.nl> wrote:

Amadeus Folego schreef op 26-5-2014 22:59:

On Mon, May 26, 2014 at 10:44:13PM +0200, Roelof Wobben 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.

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

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

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

Roelof

···

On 26 May 2014, at 22:20, Roelof Wobben <r.wobben@home.nl> wrote:

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

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 26 May 2014, at 22:20, Roelof Wobben <r.wobben@home.nl> wrote:

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

Thanks,

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: atma@convalesco.org
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

···

On 27 Μαϊ 2014, at 09:31 , Roelof Wobben <r.wobben@home.nl> wrote:

On 26 May 2014, at 22:20, Roelof Wobben <r.wobben@home.nl> wrote:

Panagiotis Atmatzidis schreef op 27-5-2014 9:41:

···

On 27 Μαϊ 2014, at 09:31 , Roelof Wobben <r.wobben@home.nl> wrote:

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 Wobben <r.wobben@home.nl> wrote:

Thanks,

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

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 Wobben <r.wobben@home.nl>:

Panagiotis Atmatzidis schreef op 27-5-2014 9:41:

On 27 Μαϊ 2014, at 09:31 , Roelof Wobben <r.wobben@home.nl> wrote:

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 Wobben <r.wobben@home.nl> wrote:

Thanks,

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

Changed it and still the syntax error message.

Roelof

--
С уважением, Шурмин Евгений

Евгений Шурмин 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

I don't know 'even?' method.
Look here about all methods of String class:

···

2014-05-27 13:50 GMT+04:00 Roelof Wobben <r.wobben@home.nl>:

Евгений Шурмин 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

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.

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

···

On Tuesday 27 May 2014 11:50:15, Roelof Wobben <r.wobben@home.nl> wrote:

Excuse the typo:

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

···

On Tuesday 27 May 2014 15:11:54, Eric MSP Veith <eveith@wwweb-library.net> wrote:

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