1
\$\begingroup\$

I am trying to clear my localstorage when I am logging out of the application. I want to write a unit test case in Jasmine to check if this task is performed when logout function is run. I am writing test cases for the first time so got stuck in the approach.

In my compoment.ts file I have a logout function:

logout() {
    location.href = "/";
     localstorage.clear();
}

spec.ts file

beforeEach(function () {
        var store = {};
        spyOn(localStorage, 'getItem').andCallFake(function (key) {
            return null;
        });
    });

I don't know if this is a correct approach to write the test case for this particular requirement or which one among unit or integration test cases is actually valid for this situation.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

If you are using any I/O (in this case, local storage), you must have fallen into an integration test.

  1. A unit test could be that when logout is executed, the call to clearing the local storage is made (using an spy on localStorage).
  2. An integration test could be that you have data in the local storage and after you have logged out, you don't.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.