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: