Fix unit test for bootstrap-slider

Xiaochen Wang
Dec 8, 2018 · 3 min read

In this blog I want wrote about How I fix the unit test for bootstrap-slider project on GitHub.

Unit testing is a small piece of code written by the developer to verify that a small, well-defined function of the code under test is correct. In general, a unit test is used to determine the behavior of a particular function under a particular condition (or scene).

Image for post
Image for post
There have a bug cause PublicMethodsSpec.js testing file always test failed In the bootstrap-slider V10.3.3

Problem:

what I did to solve this problem is I tried to read line from line 149 to 162 very carefully, and try to understand what dose this test will do. Then I found this test is going to test the formatter option. The test value was given 9 and should be return 9 as well for passing this test. However, it always return 955 by the end. So the expect value is not match to real test function returned.

Image for post
Image for post
This was the error.

Debugging:

I tried to give different value to test the setValue() function but there are always 5 and 5 attached after the expected value that returned from getValue() fuction.

Image for post
Image for post

I try to debug it in the google chrome browser, then I found a very interesting result that this function actually return the correct value. As you can see the left side picture, I give setValue() function a number 9 and getValue() function is return a number 9.

I aware of this function is just works fine so should be another error cause this issue. I uncomment all of the unit test code exclude this one and it pass the unit test WOW!!! So, it demonstrate what I’m thought.

Image for post
Image for post

This is the problem!!! Is not located in problem line but in other unit test function.

The original unit test programmer doesn’t destory the silder instance cause “generates mutiple silder instance” test failed.

PR Link: https://github.com/seiyria/bootstrap-slider/pull/888

Communication:

Image for post
Image for post
My contribution was merged to this project

My Contribution:

Image for post
Image for post
the new release version is have my name on the changelog file

You see this is just one line modification of the code to fix the bug but it actually cause my almost 3 hours to find where is the bug and search how to fix this issue. At the end, by the new release version have my name on the changelog file, I think hard debugging is worth it. 👏🏻👏🏻👏🏻

Conclusion:

example has shown that an unit testing will find many bugs at some stage of software development, and the cost of modifying them is also low in the early software development stage. In the later stages of software development, however, the discovery and modification of bugs will become more difficult and consume a lot of time and development costs. In this PR I have learned that Testing the software product as early as possible in the life cycle will ensure the best guarantee of efficiency and quality.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store