Using Jasmine to test for Triangle type

40 views
Skip to first unread message

Johnathan Mullen

unread,
Nov 6, 2016, 1:16:45 AM11/6/16
to Jasmine
Hi,

I am new to testing software and need some guidance. I am stuck on how to test what type of triangle my function would print.

describe("triangle test",function(){

  var Equilateral;
  var Isosceles;
  var Scalene;
  var side1;
  var side2;
  var side3;

  it("when all sides are equal should return Equilateral",function(){
    side1 = 1;
    side2 = 1;
    side3 = 1;

    expect(side1).tobe(side2);
    expect(side2).tobe(side3);
    expect(side3).tobe(side1);

  });

  it("when two sides are equal should return Isosceles "(){
    

    
  });
)};


JavaScript

function isTriangle(side1,side2,side3){
  
  if(side1 == side2 && side2 == side3){
   console.log("Equilateral");
  }
  else if(side1 == side2||side2 == side3||side1 == side3){
    console.log("Isosceles");
  }
  else{
    console.log("Scalene");
  }
};

isTriangle(1,9,9)
 
 

Tim Wright

unread,
Nov 6, 2016, 1:32:28 AM11/6/16
to jasmi...@googlegroups.com

Hi Jonathan,

You have a couple of options here. You could test all variants for a triangle in one test, or have seperate tests for them. Your structure implies the first (note that I'm using ES6 syntax and haven't compiled the following code):

it("should return 'Isosceles' when two sides are equal" => {
expect(isTriangle(1,9,9)).toEqual("Isosceles");
expect(isTriangle(9,1,9)).toEqual("Isosceles");
expect(isTriangle(9,9,1)).toEqual("Isosceles");
});

Note that I'm a fan of each test having one "expect" line - because each test should test exactly one thing. So personally, I'd write three tests for that function.

Tim

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Joe Chambley

unread,
Nov 6, 2016, 1:46:20 AM11/6/16
to Jasmine
It's my understanding that you want the Jasmine code, that is, the describe() ... it() to test your isTrinagle function.  So, you'll need a call like this in your it clause like:


it("when all sides are equal should return Equilateral",function(){
    side1 = 1;
    side2 = 1;
    side3 = 1;
expect(isTrigangle(side1, side2, side3).toEqual("Equilateral Triangle");
});

And then, in your function, instead of console.log, you would return "Equilateral Triangle".

This should get a the Jasmine test to pass. 

The Jasmine Introduction at https://jasmine.github.io/2.5/introduction may be helpful, too.
Reply all
Reply to author
Forward
0 new messages