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
roelof
May 26, 2014, 11:00pm
2
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.
roelof
May 26, 2014, 11:06pm
3
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 -
assert_equal “Hello World!”, greeter
roelof
May 26, 2014, 11:20pm
4
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
roelof
May 26, 2014, 11:37pm
5
Which ruby version are you using?
On 2.1.1 it runs fine.
roelof
May 26, 2014, 11:41pm
6
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
roelof
May 26, 2014, 11:41pm
8
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
roelof
May 27, 2014, 9:42am
10
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
roelof
May 27, 2014, 9:47am
11
Panagiotis A. schreef op 27-5-2014 9:41:
Thanks,
Roelof
Changed it and still the syntax error message.
Roelof
roelof
May 27, 2014, 10:34am
12
Oh…
“:” → “::”
Try this code:
class Testgreeter < Test::Unit::TestCase
end
2014-05-27 11:46 GMT+04:00 Roelof W. [email protected] :
roelof
May 27, 2014, 11:50am
13
??? ??? 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
roelof
May 27, 2014, 3:13pm
14
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
roelof
May 27, 2014, 11:58am
15
I don’t know ‘even?’ method.
Look here about all methods of String class:
2014-05-27 13:50 GMT+04:00 Roelof W. [email protected] :
roelof
May 27, 2014, 3:43pm
16
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
roelof
May 28, 2014, 1:14am
17
Curious. How do you test a function embedded in another function?