A
A
AlexNomad2012-12-25 17:36:40
JavaScript
AlexNomad, 2012-12-25 17:36:40

Simple unit tests?

Why are unit tests so awkward to write?!
Maybe someone saw simpler rules for describing tests, for example, something like this:

/**
@test(1,2) = 3
@test(2,3) = 5
**/
function foo(x,y){
    return x+y
}


/**
@test() = 3 #this={x:2,y:3}
@test() = 5 #this=url(/src/test/method1.json)
**/
function method1(){
    return this.x+this.y
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
egorinsk, 2012-12-26
@AlexNomad

I have already seen tests in annotations somewhere. As I understand it, they become inconvenient as soon as you need to test functions a little more complicated than the addition function (by the way, I send the rays of diarrhea to all the authors of test articles who cannot come up with a good convincing example and test rubbish like addition functions).
Specifically in your case, are you testing what you need? Are you by chance just testing a simple and obvious code of a few lines, which makes no sense to test?

X
XimikS, 2012-12-25
@XimikS

> simpler
than what?
In general, there is a good bunch of Mocha and Chai .
For Rails, if anyone is interested, use Konach :)

S
Sergey, 2012-12-25
Protko @Fesor

pivotal.github.com/jasmine/ - it seems to me that there is nothing better.

A
AlexNomad, 2012-12-27
@AlexNomad

Thanks for the answer.
Indeed, I often think about this kind of testing when I write a lot of short functions, often in libraries. At the same time, I want to immediately check its correctness, including on the boundary data

    /**
     * Возвращает точку между двумя заданными
     * @param start Point
     * @param end Point
     * @param koef Number (optional=0.5) коэффициент расстояния (0=start, 1=end, 0.5=середина)
     * @return Point
     * @test(new Point(0,0), new Point(10,10), 0) = {x:0, y:0}
     * @test(new Point(0,0), new Point(10,10), 1) = {x:10, y:10}
     * @test(new Point(0,0), new Point(10,10)) = {x:5, y:5}
     */
    function getMiddlePoint(start, end, koef) {
        if(koef === undefined) koef=0.5;
        return new Point ( end.x * koef + start.x * (1 - koef), end.y * koef + start.y * (1 - koef));
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question