javascript - To what extent can we refactor unit tests -
when working jasmine
testing framework, have come across code snippet expect
written in sub-function called in it()
, not in it()
itself. reason behind writing is, trying compare similar objects , refactored
test code , moved expect
sub-function. now, don't have expect
in it()
rather have method call has expect
.
describe("bla bla", function() { //for nunit, junit guys, testsuite() function somefunc() { //this [test] //do stuff , generate object expect(someobject).toequal(otherobject); //expect assert } it("some case", function() { //do stuff somefunc(); }); it("some other case", function() { //do other stuff somefunc(); }); });
now, kind of test code encouragable? can have it()
without expect
?
i argue 2 examples below both readable. if assertexpected() did kind of setup otherobject
first example more readable.
describe("bla bla", function() { //for nunit, junit guys, testsuite() function assertexpected() { //this [test] //do stuff , generate object expect(someobject).toequal(otherobject); //expect assert } it("some case", function() { // arrange var someobject; // act someobject = testmethod('foo'); // assert assertexpected(); }); it("some other case", function() { // arrange var otherobject, someobject = testmethod('foo'); // act someobject = testmethod('foo'); // assert assert(someobject(foo)).toequal(otherobject); }); });
i think deciding factor here should deciding on house style , sticking it. there's no clear winner here , question isn't question so.
Comments
Post a Comment