Ad
  • Default User Avatar

    You're quite right. This is some code I tried on many katas on codewars to see how many of them I can solve with a generic solution like this. I create an object and I'm changing the eigenclass of that object by overwriting the == method. Then decompose(y) == x will return true or any values x and y. Another even more generic solution included the use of method_missing to ensure that every method called on the object also returned an equality object. This ensured that even testing for derived values like the length of an array would not fail this solution.

    The point why this actually works isn't so much seen here though. Any test framework has to check for equality at some point like this:

    if actual == expected
      test_is_passing
    else
      test_is_failing
    end
    

    So this solution just fakes an always passing test (see https://github.com/Codewars/kata-test-framework-ruby/blob/master/framework.rb at line 147). There is no way to prevent that kind of solution, but some way to make it harder. I think there should be some tests using Test.expect(expected == actual) then you can't simply modify the comparison of the object you return, but you'd have to monkey patch the class of what is expected. When the kata would test for several values like length of the array, specific elements, iteration over it, and such it would be a major hazzle figuring out everything.

    Just a little reminder that this solution is of course a fake. It doesn't solve the actual problem, it just demonstrates that green tests aren't a good metric, because no actual behavior might be implemented (doesn't have to be obvious like here though).