Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand Jasmine's toHaveBeenCalled() matcher

I am new to jasmine here is my src file in which i create Auth class

function Auth() {
}

Auth.prototype.isEmpty = function(str) {
    return (!str || 0 === str.length);
}

Auth.prototype.Login = function (username , password) {
    if (this.isEmpty(username) || this.isEmpty(password)) {
        return "Username or Password cann't be blank ";
    }
    else {
        return "Logged In !";
    }
}

now i want to test jasmine's toHaveBeenCalled() matcher . Here is what i write

it("should be able to Login", function () {
    spyOn(authobj);
    expect(authobj.Login('abc', 'abc')).toHaveBeenCalled();
});

but it says that undefined() method does not exist

like image 339
Ancient Avatar asked May 28 '13 08:05

Ancient


People also ask

What is toHaveBeenCalled in Jasmine?

toHaveBeenCalled() Matcher The toHaveBeenCalled() matcher verifies whether the spied method has been called or not. It returns true if the spy was called.

Which of the following Jasmine matchers can be used for matching a regular expression?

It checks whether something is matched for a regular expression. You can use the toMatch matcher to test search patterns.

What are matchers in Jasmine?

Jasmine is a testing framework, hence it always aims to compare the result of the JavaScript file or function with the expected result. Matcher works similarly in Jasmine framework. Matchers are the JavaScript function that does a Boolean comparison between an actual output and an expected output.

How do you use spyOn in Jasmine?

describe("Example Of jasmine Spy using spyOn()", function() { it('uses the dictionary to say "hello world"', function() { var dictionary = new Dictionary; var person = new Person; spyOn(dictionary, "hello"); // replace hello function with a spy spyOn(dictionary, "world"); // replace world function with another spy ...


2 Answers

Looking at your use-case, I can't recommend using toHaveBeenCalled here. toHaveBeenCalled is useful in cases where you want test callbacks (async) or in combination with mocks.

Consider everything that happens within Auth.prototype.Login as implementation detail which is not visible to the "outerworld". You shouldn't test implementation details. Which triggers two questions.

Why shouldn't I test implementation details?

It makes refactoring hard. Let's say you want to replace Auth.prototype.isEmpty for some reasons by underscore.isEmpty. Some days later you decide to replace underscore completely by lodash. This would force you to change your test three times. Consider everything that prevents you from refactoring easily as a "no-go".

What shall I test instead?

Public API. Everything that is visible to the "outerworld". Which is in your case "Logged In !" and "Username or Password cann't be blank ".

Which results in 3 tests:

describe('Login', function() {

 it('returns "success" string when username and password are not empty', function() {
   expect(new Auth().Login('non-empty', 'non-empty')).toBe('Logged In !');
 });

 it('returns "failure" string when username is empty', function() {
   expect(new Auth().Login('', 'non-empty')).toBe('Username or Password cannot be blank');
 });

 it('returns "failure" string when password is empty', function() {
   expect(new Auth().Login('non-empty', '')).toBe('Username or Password cannot be blank');
 });

});
like image 76
basecode Avatar answered Oct 25 '22 21:10

basecode


EDIT: Look at basecode answer for a better approach


From the docs, you should use it like the following:

spyOn(foo, 'setBar');

it("tracks that the spy was called", function() {
  expect(foo.setBar).toHaveBeenCalled();
});

So you should write:

it("should be able to Login", function () {
  spyOn(authobj, 'isEmpty');  
  authobj.Login('abc', 'abc');  
  expect(authobj.isEmpty).toHaveBeenCalled();
});
like image 21
Alberto Zaccagni Avatar answered Oct 25 '22 20:10

Alberto Zaccagni