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.

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Apr 11, 2019 at 5:52
\$\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.
answered May 21, 2019 at 21:09
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.