Question

I'm trying to find the best way to test an object params is not alterate by a function.

Imagine the function does nothing:

function underTest (someObject) {
    //It does nothing
}

Now I wanna test that the arg is not midified by the function. I KNOW nearly nobody will make tests like that but I'm an integrist (and a noob ^^)

it("Do not modify params", function () {
    var myObj = { property : 1, other : 3};
    underTest(myObject);
    //??? So
});

Question is: how would you achieve that?

Repeating the object?

it("Do not modify params", function () {
    var myObj = { property : 1, other : 3};
    underTest(myObject);
    assert.deepEqual({ property : 1, other : 3}, myObj);
});

Using a framework to clone it before using in function?

it("Do not modify params", function () {
    var myObj = { property : 1, other : 3};
    var clone = clonerFunction(myObj);
    underTest(myObj);
    assert.deepEqual(clone, myObj);
});

A solution wih sinonJS (Already used for spy/stub on function) ?

Was it helpful?

Solution

To my mind, if an object is not a large one it would be better to clone it (or parts of it), there is by the way a useful (yet not documented) function in nodejs utils which is '_extend' that does the job for you.

But really, I feel like you should not do that (or implementing any kind of tests) within the bounds of your module and only pass copies of objects in cases when you expose them for other modules to use. This is because, ideally, you would know how your code works and would not misuse it, thus avoiding slowing it down by numerous validity checks and only wary about introduced errors from outside.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top