Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

So you would write test cases for all the methods and constructors for a class like this:

  class Foo{
    public Foo(initialValue) {
        this.value = initialValue
    }
  
    public setValue(value) {
        this.value = value
    }
  
    public getValue() {
        return this.value;
    }
  }
What's the point, what are you testing? That the language's most basic operations still work?


You'd test those cases. This way, if you'll change your code to be

``` public setValue(value) { this.value = value + 5 } ```

then your tests will start to fail.

Treat tests as a contract.


Can't tell if serious...


I'm serious. Of course you don't test that language operations work, but you're testing that given method does what it's supposed to do. In this case your method sets value for property on model. It doesn't matter you're doing it via assignment - you could be doing it by any other ways. You want to test that for given model after calling that method your models property will change to given value. This way, if you'll change the implementation of setValue your test will still succeed. If it'll start doing something else, they will fail. And of course, this method can be used in your feature tests, so those will start failing too (but that's beside the point, I guess)

Of course it's also a balancing act - should you immediately write test for this? I try to.


They are. If you don't get it then you're doing it wrong.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: