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 specs to your Jasmine test


To write specs for a given requirement, let's consider the following example of <ABC> company.

<ABC> is a product-based company that develops cutting edge software/products for sales and inventory control systems. Currently, they have one base product that offers all the standard features required of a sales and inventory system (for example, generating sales invoice, sales return/issue, vendor analysis, billing management, budgeting, finance, stock update, and so on). They also customize base products as per customers' specific needs. Recently, the <ABC> company has provided software to a spare parts company and the customer is performing acceptance testing for the inventory system.

"As a Store Administrator, I want to update stock on every new transaction so that I can get the balance/stock in hand for further usage."

Let's consider some scenarios in the current context, that is, updating inventory stock in the event of any new transaction:

  • Scenario-1: Inventory Stock should be updated on account of item(s) sale or issue of item(s)

  • Scenario-2: Inventory stock should be updated on return of any item(s)

  • Scenario-3: Inventory stocks should be updated on receiving/procuring new item(s)

How to do it…

To write specs to a Jasmine test, perform the following steps:

  1. First, you need to create a spec file under the /spec folder. Create the InventoryStock_spec.js file and code the following lines:

    describe("Inventory Stock", function() {
    //Scenario – 1
      
    });
  2. Next, use the following code to define specs:

    describe("Inventory Stock", function() {
    //Scenario – 1
        it("Inventory Stock should be updated on sale/issue of an item", function() {
    
        });
      });
  3. Now, to run the spec defined in the previous step, we need to add the reference of the spec file (that is, InventoryStock_spec.js) to the Jasmine runner (that is, SpecRunner.html file):

    <!-- include spec files here... -->
      <script type="text/javascript" src="spec/InventoryStock_spec.js"></script>
  4. To execute the test suite, open the Jasmine runner in a browser and you will see the spec results, as shown in the following screenshot:

    You can see two things from the execution results:

    • The spec is prefixed with SPEC HAS NO EXPECTATION.

    • The spec passes even if we do not specify any expectation within the it block. In Jasmine, we need to implement an assertion to make the spec pass or fail. An assertion is a comparison between two values/expression that results in a Boolean value. A spec will only be considered passed if the assertion returns the Boolean value as true.

  5. Next, use the following code to optimize step 2:

          describe("Inventory Stock", function() {
            //Scenario - 1
    it("Inventory Stock should be updated on sale of item", function() {
    
              });
    it("Inventory Stock should be updated on issue of an item within organization", function() {
    
              });
          });

    In the preceding code snapshot, you can notice that we further divided the spec into two specs where the first spec represents a sale and the other spec is for the issuing of an item. Now, both the specs represent unique behavior.

    Tip

    It is highly recommended to refactor the requirement up to granular level. This will help you to analyze test execution results. Moreover, you (and other stakeholders) can easily identify root causes and map precisely the failed specs with application code.

  6. Next, let's use the following test code to implement the specs functionality:

          describe("Inventory Stock", function() {
          //Scenario - 1
    it("Inventory Stock should be updated on sale of item", function() {
                var stockinhand_item1=11;
                var item1 = 1;
                var transaction = 'SALE';
                expect(stockinhand_item1-item1).toEqual(10);
              });
    it("Inventory Stock should be updated on issue of an item within organization", function() {
                var stockinhand_item1=11;
                var item1 = 1;
                var transaction = 'ISSUE';
                expect(stockinhand_item1-item1).toEqual(10);
              });
          });
  7. Now when you run the spec file, you will see that both the specs pass for scenario 1, as shown in the following screenshot:

  8. Now, use the following code to define and implement the specs for scenario 2 and scenario 3:

    describe("Inventory Stock", function() {
      //Scenario - 1
      it("Inventory Stock should be updated on sale of item", function() {
          var stockinhand_item1=11;
          var item1 = 1;
          var transaction = 'SALE';
          expect(stockinhand_item1-item1).toEqual(10);
        });
        it("Inventory Stock should be updated on issue of an item within organization", function() {
          var stockinhand_item1=11;
          var item1 = 1;
          var transaction = 'ISSUE';
          expect(stockinhand_item1-item1).toEqual(10);
        });
        //Scenario - 2
        it("Inventory Stock should be updated on return of any item", function() {
          var stockinhand_item1=11;
          var item1 = 1;
          var transaction = 'SALE RETURN';
          expect(stockinhand_item1+item1).toEqual(12);
        });
        //Scenario - 3
        it("Inventory Stock should be updated on receiving or procuring new item", function() {
          var stockinhand_item1=11;
          var item1 = 1;
          var transaction = 'PROCUREMENT';
          expect(stockinhand_item1+item1).toEqual(12);
        });
    });
  9. Finally, run the spec file (InventoryStock_spec.js) using the Jasmine runner. You will see the test execution results, as shown in the following screenshot indicating the success of all four specs:

How it works...

Let's understand what we did throughout this recipe.

In step 1, we created a spec file and defined the name of the test suite corresponding to the functionality, which is currently being tested. In the present scenario, we named it as Inventory Stock.

In steps 2 to 4, we created the spec to define scenario 1 and executed the scenario using the Jasmine runner. In this scenario, we defined specs to validate whether the stock is being updated (or not) on account of the sale of an item or the issue of an item to a person/department within the organization.

In step 5, we further refactored the specs of scenario 1 to make them more understandable and granular.

In steps 6 and 7, we implemented the test code for scenario 1 corresponding to specs.

In steps 8 and 9, following the same pattern, we implemented the test code for scenarios 2 and 3.

See also

  • To gain a deeper understanding about how to design and write specs, refer to the recipe Defining nested suites to write more meaningful specs in Chapter 2, Jasmine with TDD and BDD Processes.