Frontend/Jasmine

Why unit testing? why jasmine?

ej503 2023. 3. 1. 11:21

 

 

What?

 

  • Testing individual units of code.
  • Unit is the smallest testable part (function).

 

Why?

 

  • Trust changes you make on existing (already tested) code.
  • Code quality
  • Changes occur quickly
  • Documents your own code
  • Automation

 

How?

 

  • Comes out of the box with everything you need to test your code!

 

 

 

First, let's try to test our HTML Document related code.

 

 

  1. Create a folder named "1. First Spec" and lib.
  2. Go to Jasmine git repository, and download a zip file of release.
  3. Move "boot.js", "jasmine-html.js", "jasmine.css", "jasmine.js" to our lib folder.
  4. Create index.html in the root folder.
  5. Write down the basic HTML like below.
<html>
    <head>
        <meta charset="utf-8">
        <title>Your first spec</title>
        <link rel="stylesheet" href="lib/jasmine.css">
            <script type="text/javascript" src="lib/jasmine.js"></script>
            <script type="text/javascript" src="lib/jasmine-html.js"></script>
            <script type="text/javascript" src="lib/boot.js"></script>
    </head>
</html>

 

Those Jasmine related script and style will create the Jasmine UI.

Let's add the simple script to test our title.

 

<html>
    <head>
        <meta charset="utf-8">
        <title>Your first spec</title>
        <link rel="stylesheet" href="lib/jasmine.css">
            <script type="text/javascript" src="lib/jasmine.js"></script>
            <script type="text/javascript" src="lib/jasmine-html.js"></script>
            <script type="text/javascript" src="lib/boot.js"></script>

            <script type="text/javascript">
                it('should have the correct title', function() {
                    expect(document.title).toBe('Your first spec');
                });
            </script>
    </head>
</html>

 

 

'Frontend > Jasmine' 카테고리의 다른 글

Jasmine toBe vs toEqual  (0) 2024.06.27
Difference of spyOn, callThrough, and callFake  (0) 2024.06.16
Creating your calculator and Testing with Jasmine  (2) 2023.03.04