Book Image

Jasmine Cookbook

By : Munish Kumar
Book Image

Jasmine Cookbook

By: Munish Kumar

Overview of this book

Table of Contents (16 chapters)
Jasmine Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Adding expectations and matchers to the test


Inside the it block, you can write all the test code that is required to test the application/JavaScript code by applying assertions using expectations and matchers. Expectations are built with an expect function, which takes a value called actual. It is further chained with the matcher function(s), which takes the expected value. Each matcher implements a Boolean expression depending on the actual and expected value. It is responsible for reporting to Jasmine whether the expectation is true or false. Jasmine passes or fails the spec on the basis of the Boolean value returned by the matcher. In this recipe, you will learn how assertions are applied using the toBe matcher. You will also learn how negative assertions are applied.

To understand this recipe, assume that you are developing a bank application to track details of fixed deposit, recurring deposit, and all other financial transactions.

"As a finance administrator, I want to track all financial transactions so that I can categorize them for further assessment/processing."

Let's consider the following scenarios in the current context, that is, all financial transactions should be tracked and categorized:

  • Scenario-1: Deposit should be of the fixed Deposit (FD) type on locking amount for a fix period

  • Scenario-2: Deposit should be of the Recurring Deposit (RD) type for an amount deposited with regular frequency (that is, monthly, quarterly, half-yearly, yearly, and so on)

How to do it…

You need to perform the following steps to apply the toBe matcher on these scenarios:

  1. Create the Deposit_spec.js file under the /spec folder and code the following lines:

    describe("Bank Deposit ", function() {
    //Scenario 1
      
    });
  2. Next, use the following code to define specs for scenario 1 and scenario 2:

    describe("Bank Deposit",function(){
      //Scenario 1
      it("should be considered as FD on locking amount for a fixed period", function(){
    
      });
      //Scenario 2
      it("should be considered as RD on depositing amount on regular frequency", function(){
    
      });  
    });
  3. To implement scenario 1 and scenario 2, we need JavaScript code. So, create the Deposit.js file, put it under the /src folder, and use the following code:

    function Deposit(Frequency) {
      this.Type= Frequency;
    };
    
    Deposit.prototype.BankDeposit = function(){
      switch (this.Type) {
      case "FIX" :  
            return "FD";
            break;
      case "RECURRING" :  
            return "RD";
            break;
      };
    };
  4. Next, use the following code to implement specs for scenario 1 and scenario 2:

    describe("Bank Deposit",function(){
      //Scenario -1
      it("should be considered as FD on locking amount for a fix period", function(){
        var MyDeposit = new Deposit("FIX");
        DepositType = MyDeposit.BankDeposit();
        expect(DepositType).toBe("FD");
      });
      //Scenario -2
      it("should be considered as RD on depositing amount on regular frequency", function(){
        var MyDeposit = new Deposit("RECURRING");
        DepositType = MyDeposit.BankDeposit();
        expect(DepositType).toBe("RD");
      });  
    });
  5. Now, run the spec file (Deposit_spec.js) using the Jasmine runner and you will see that tests pass for both the scenarios, as shown in the following screenshot:

  6. Now, to apply negative assertions on both of the scenarios, consider the following code:

    describe("Bank Deposit",function(){
      //Scenario -1
      it("should be considered as FD on locking amount for a fix period", function(){
        var MyDeposit = new Deposit("FIX");
        DepositType = MyDeposit.BankDeposit();
        expect(DepositType).toBe("FD");
        expect(DepositType).not.toBe("FD");
      });
      //Scenario -2
      it("should be considered as RD on depositing amount on regular frequency", function(){
        var MyDeposit = new Deposit("RECURRING");
        DepositType = MyDeposit.BankDeposit();
        expect(DepositType).toBe("RD");
        expect(DepositType).not.toBe("RD");
      });  
    });

    In the preceding code snapshot, notice that we implemented the negative assertion by chaining the call to expect with a not before calling the matcher.

  7. Now, run the spec file (Deposit_spec.js) using the Jasmine runner and you will see that it indicates that both the tests fail, as shown in the following screenshot:

    In the preceding screenshot, notice that we have provided wrong values corresponding to negative assertions.

  8. Now, use the following code to pass both the tests:

    describe("Bank Deposit",function(){
      //Scenario -1
      it("should be considered as FD on locking amount for a fix period", function(){
        var MyDeposit = new Deposit("FIX");
        DepositType = MyDeposit.BankDeposit();
        expect(DepositType).toBe("FD");
        expect(DepositType).not.toBe("RD");
        expect(DepositType).not.toBe("Any value Other than 'FD' ");
      });
      //Scenario -2
      it("should be considered as RD on depositing amount on regular frequency", function(){
        var MyDeposit = new Deposit("RECURRING");
        DepositType = MyDeposit.BankDeposit();
        expect(DepositType).toBe("RD");
        expect(DepositType).not.toBe("FD");
        expect(DepositType).not.toBe("Any value Other than 'RD' ");
      });  
    });
  9. Finally, run the spec file (Deposit_spec.js) using the Jasmine runner and you will see that it indicates that both the tests pass, as shown in the following screenshot:

How it works...

In step 1 and step 2, we defined the name of the suite and specs for scenario 1 and scenario 2.

In step 3, JavaScript code is provided to implement Jasmine tests for both the scenarios. Here, we defined the object construction function with one parameter (that is, frequency) to identify the type of deposit. Also, we created a BankDeposit() function for the deposit object using JavaScript prototype property.

In step 4, we implemented the test code corresponding to specs within the it block to test the code. First, we created the object of deposit and then invoked the BankDeposit() function of the deposit object to get the deposit type. Finally, we implemented the assertion to compare the actual and expected value using the toBe matcher.

In steps 6 through 9, we implemented a negative assertion by chaining the call to expect with a not before calling the matcher. We also looked at how Jasmine tests pass/fail using the different values with negative assertion.