Created
November 26, 2015 08:16
-
-
Save amiceli/63893cf12a6a9bdd3011 to your computer and use it in GitHub Desktop.
Some JavaScript testing with Jasmine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<!-- jasmine --> | |
<link rel="shortcut icon" type="image/png" href="lib/jasmine/lib/jasmine-2.3.4/jasmine_favicon.png"> | |
<link rel="stylesheet" type="text/css" href="lib/jasmine/lib/jasmine-2.3.4/jasmine.css"> | |
<script type="text/javascript" src="lib/jasmine/lib/jasmine-2.3.4/jasmine.js"></script> | |
<script type="text/javascript" src="lib/jasmine/lib/jasmine-2.3.4/jasmine-html.js"></script> | |
<script type="text/javascript" src="lib/jasmine/lib/jasmine-2.3.4/boot.js"></script> | |
<script type="text/javascript" src="lib/jasmine/lib/jasmine-2.3.4/console.js"></script> | |
<script type="text/javascript" src="lib/jasmine/lib/jasmine-2.3.4/mock-ajax.js"></script> | |
<script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script> | |
<script src="lib/models/User.js"></script> | |
<script src="spec/myTest/MySpec.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("MySpec", function() { | |
var user = new User('Pablo', 'Escobar') | |
beforeEach(function() { | |
spyOn(user, 'getDescription'); | |
spyOn(user, 'setAge'); | |
user.getDescription(); | |
user.setAge(19); | |
}) | |
it('fullName must be a name and lastName concat', function() { | |
user = new User('Toni', 'Montana'); | |
expect(user.fullName()).toEqual('Toni Montana'); | |
}); | |
it('age must be defined', function() { | |
user = new User('Toni', 'Montana', 18); | |
expect(user.getAge()).toEqual(18); | |
}); | |
it('an error must be returned if age is undefined', function() { | |
var user = new User('Toni', 'Montana'); | |
expect(user.getAge).toThrowError("Age is undefined"); | |
}); | |
it('tracks spy calls', function() { | |
expect(user.getDescription).toHaveBeenCalled(); | |
}); | |
it('tracks spy called without arguments', function() { | |
expect(user.getDescription).toHaveBeenCalledWith() | |
}) | |
it('user age must initialize with an integer', function() { | |
expect(user.setAge).toHaveBeenCalledWith(jasmine.any(Number)); | |
}) | |
describe('user ajax request test', function() { | |
var user, failFn, successFn; | |
beforeEach(function() { | |
user = new User('Pablo', 'Escobar') | |
doneFn = jasmine.createSpy('success'); | |
failFn = jasmine.createSpy('fail'); | |
jasmine.Ajax.install(); | |
}); | |
afterEach(function() { | |
jasmine.Ajax.uninstall(); | |
}); | |
it('test if request is correct', function() { | |
user.getDescriptionFromGithub(); | |
expect(jasmine.Ajax.requests.mostRecent().url).toBe('https://api.github.com/users/' + user.name); | |
}); | |
}); | |
describe('test ajax request without mock', function() { | |
var user, failFn, successFn, called = false; | |
beforeEach(function(done) { | |
user = new User('Pablo', 'Escobar') | |
doneFn = jasmine.createSpy('success'); | |
failFn = jasmine.createSpy('fail'); | |
user.name = 'xsxsxsxsxsxs'; | |
user.getDescriptionFromGithub(doneFn, function() { | |
failFn(); | |
done(); | |
}); | |
}); | |
it("return an error when request failed", function() { | |
expect(failFn).toHaveBeenCalled(); | |
}); | |
}) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function User(name, lastName, age) { | |
this.name = name; | |
this.lastName = lastName; | |
this.age = age; | |
} | |
User.prototype.fullName = function() { | |
return this.name + " " + this.lastName; | |
} | |
User.prototype.getAge = function() { | |
if(this.age) { | |
return this.age; | |
} | |
throw new Error("Age is undefined"); | |
} | |
User.prototype.getDescription = function() { | |
return 'User : ' + this.fullName(); | |
} | |
User.prototype.setAge = function(age) { | |
this.age = age; | |
} | |
User.prototype.getDescriptionFromGithub = function(successFn, failFn) { | |
$.ajax({ | |
url : 'https://api.github.com/users/' + this.name, | |
type : 'GET', | |
success :successFn, | |
error : failFn | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment