Reputation: 7185
I am working in reverse in a system lifecycle. A few months ago I wrote a large javascript library. I then had to make it all objective and now, I have to write unit tests for it. I am using Maven and have the jasmine-maven-plugin
in my pom.xml. The problem I am having is what should I be writing tests for, and how many.
This first example is simple. The function takes a string and returns it with the first letter capitalised.
var toolsFn = {
capitaliseFirstLetter: function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
},
and so my unit test it:
describe("toolsFn - capitaliseFirstLetter", function() {
it("capitalises the first letter of a given string", function() {
expect(toolsFn.capitaliseFirstLetter("hello World!")).toBe("Hello World!");
});
});
However, I am unsure what I should do for many of my other methods. The majority of them deal with the html code such as changing a tab, showing a notification, disabling/enabling controls. Should I just expect the method toHaveBeenCalled
or is there more to it than that?
Please check the following examples which changes tabs, loads a given tab and hides a notification;
tabsFn = {
changeTab: function() {
$(tabButtons).addClass('inactive');
$(tabContent).hide();
$(this).removeClass('inactive');
var tab = $(this).attr('tab');
$('.tab-content-' + tab).show();
return false;
},
loadTab: function(tab) {
$(tabButtons).addClass('inactive');
$(tabContent).hide();
$('[tab~="' + tab + '"]').removeClass('inactive').removeAttr('disabled');
$('.tab-content-' + tab).show();
},
messageFn = {
hideNotification: function(time) {
$(messageFn.notificationBar).stop(true, true).fadeOut(time);
},
Any clarification is much appreciated.
Upvotes: 1
Views: 803
Reputation: 11134
The majority of them deal with the html code such as changing a tab, showing a notification, disabling/enabling controls.
What you want to verify for that is if the resulting behavior is correct (ex.: content changed). This is typically done with integration test and Jasmine isn't the best tools for integration testing. Tools such as Selenium will do a better job that kind of test.
Just don't fall in trap of starting to test if a specific function is called to verify if the functionality is still working properly. The internal of that functionality can change and call other function and that doesn't mean it's now broken. So that's why it's better to do it at an higher level (integration testing).
The problem I am having is what should I be writing tests for, and how many
Normally, you write unit test for each function that aren't meant to change the state of your application. Your capitalization function is a good example of a function that is unit tested.
As to how many, it depends of the time you have now to write unit test and the time you will have to maintain them in the future. You also have to keep in mind the usefulness of each test. If what you are testing is too trivial there may be no value in doing a unit test for that. If you have a good amount of time, a good technique for testing a function is to have a test case for each edge cases (in your first letter uppercase example, a good example of that would be an empty string, null or undefined) and a test for a general case. This will result in about 2-4 tests per function. With that you usually have a pretty good coverage of your code.
Upvotes: 2