Jump to content
  • Advertisement

Javascript Specs for Node.js Calculator. Jasmine JS5

8Observer8

1705 views

Let's write an executable documentation for server side calculator.

Specifications:

  • "Add(a, b)" method must to sum positive numbers. Specification name: Add_SumPositiveNumbers_ReturnsSum
  • "Sub(a, b)" method must to subtract positive numbers. Specification name: Sub_SubtractPositiveNumbers_ReturnsSub

Instruction:

  • Create the "calculator-nodejs-jasmine-es5" folder
  • Run these commands to install Jasmine locally:
npm init -y
npm install --save-dev jasmine
  • Create the "jasmine.json" file in the "calculator-nodejs-jasmine-es5" folder
  • Copy the content for the "jasmine.json" file from the link: https://jasmine.github.io/setup/nodejs.html and make some changes, see the content below:

jasmine.json

{
    "spec_dir": "src_specs",
    "spec_files": [
        "**/*_tests.js",
        "!**/*nospec.js"
    ],
    "random": false
}

Open the "package.json" file and add the command to run tests:

    "scripts": {
        "test": "node node_modules/jasmine/bin/jasmine.js  --config=jasmine.json"
    },

Try to run tests. Enter the command:
 

npm test

You will see a message that "No specs found"

  • Create the "src_shared" folder. Create the "calculator.js" file the the "src_shared" folder
  • Create the "src_specs" folder. Create the "calculator_tests.js" file the the "src_specs" folder
  • Add our specs described above to the "calculator_tests.js" file:

calculator_tests.js

var Calculator = require("../src_shared/calculator");

describe("Calculator", function()
{
    it("Add_SumPositiveNumbers_ReturnsSum", function()
    {
        // Arrange
        var calculator = new Calculator();
        var a = 5;
        var b = 2;
        var expectedSum = 7;
 
        // Act
        var actualSum = calculator.Add(a, b);
 
        // Assert
        expect(actualSum).toEqual(expectedSum);
    });
 
    it("Sub_SubtractPositiveNumbers_ReturnsSub", function()
    {
        // Arrange
        var calculator = new Calculator();
        var a = 5;
        var b = 2;
        var expectedSub = 3;
 
        // Act
        var actualSub = calculator.Sub(a, b);
 
        // Assert
        expect(actualSub).toEqual(expectedSub);
    });
});
  • If you will run tests using the command "npm test" then you will see the message:
Quote

ReferenceError: Calculator is not defined

Let's implement these methods and run tests using the command "npm test". You will see that the tests are passed.

calculator.js

var Calculator = function()
{
 
};
 
Calculator.prototype.Add = function(a, b)
{
    return a + b;
};
 
Calculator.prototype.Sub = function(a, b)
{
    return a - b;
};

module.exports = Calculator;

 

calculator-nodejs-jasmine-es5.png



1 Comment


Recommended Comments

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By phenom120
      I am developing a simply 2d physics engine and I' ve read the source code of box2d and matter.js. I found some insteresting thing on finding contact point:
      Box2d use indicent edge, reference edge and clip to determine a contact point. It's very complex method and hard to understand.
      However, matter.js use a very simply method to do the same thing. just find the vertex that contained by opposite polygon with hill-climbing:
      var verticesB = SAT._findSupports(bodyA, bodyB, collision.normal), supports = []; // find the supports from bodyB that are inside bodyA if (Vertices.contains(bodyA.vertices, verticesB[0])) supports.push(verticesB[0]); if (Vertices.contains(bodyA.vertices, verticesB[1])) supports.push(verticesB[1]); // find the supports from bodyA that are inside bodyB if (supports.length < 2) { var verticesA = SAT._findSupports(bodyB, bodyA, Vector.neg(collision.normal)); if (Vertices.contains(bodyB.vertices, verticesA[0])) supports.push(verticesA[0]); if (supports.length < 2 && Vertices.contains(bodyB.vertices, verticesA[1])) supports.push(verticesA[1]); } and it works well in demo.
      My question is: why does box2d use such complex method? It's clear that matter.js's method is better. Or there are some potential shortcomings in matter.js's method?
       
    • By Onny98
      Hello, im trying to learn new techniques to implement in my Unity3D projects and i have been looking for a particular answer about the way Hollow Knight's creators made to give background sprites that feeling of depth. I was thinking about just blurring them out in ps but someone suggested me increasing saturation, white degrading and changing opacity. Im unsure what is exactly the way to recreate this and i would like to know aswell if there is a tool in Unity3D that can make this automatically. Thank you so much I will leave a image showing what i mean exactly.

    • By JohnElliott
      Greetings all.
      I'm a researcher from Portugal, with the multimedia department of CIEBA, and I'm currently writing my PhD thesis on videogame composers' expansion of knowledge into areas of sound design, implementation, programming, and soft skills.
      I come to ask for bibliography, books and articles, on the process of creating videogames, particularly about differences between indie and AAA production (I am focusing on indie).
      So far I've used O'Donnell's "Developer's Dilemma", a great read I suggest to all, as well as my own experiences, but I am in need of more sources.
      Thanks in advance!
    • By leePerak
      Hi guys,
       Check this new free Android game I just released today in Play Store - Grazomvity.  Specially designed to be played one handed while standing in train, bus etc.   
       https://play.google.com/store/apps/details?id=com.adriasys.grazomvity
       There are no make-believed, over-hyped, fabricated comments and ratings from friends or family. YOU be the FIRST to rate and comment.
       As a bonus, the winner gets free temporary upgrade and eventually can see the entire game source code.
      Your feedback is much appreciated.
      Thanks guys,
      Lee



    • By rontrek
      Hi, I'm trying to implement a collision on ImpactJS entities that both will be stopped when collided and being pushed.
      This is similar to player to player collision on top down games such as Battle City where you can't push the other player and vice versa.
      All collision type combination does not work. It still pushes the other and FIXED vs FIXED is being ignored.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!