Shefali Khanna
Shefali Khanna

Reputation: 1

Testing two expects of same function using one 'it'

I am new to Jasmine. I have faced this particular issue as follows: - Let's suppose the code i want to test is:

myTestFunction: function () {
  if (this.field) {
    return true;
  }
  else
     return false;
}

TestCase:

it('myTestFunction', function () {
   this.obj.field = true;
   expect(obj.myTestFunction() ).toEqual(true);
   this.obj.field = false;
   expect(obj.myTestFunction() ).toEqual(false);
})

Now if my first expect fails, the whole test case fails and my other expect are not even considered. Is there any way that if one of the expect fails, the other works fine and test case shows one expect to be failed and other to be passed, without writing seperate test cases for if and else ? Thank you

Upvotes: 0

Views: 216

Answers (1)

Cerbrus
Cerbrus

Reputation: 72887

Each it should be a separate case. So, if you want to test the true and false variants separately, you can't have them in the same it:

it('myTestFunction true', function () {
  this.obj.field = true;
  expect(obj.myTestFunction() ).toEqual(true);
})

it('myTestFunction false', function () {
  this.obj.field = false;
  expect(obj.myTestFunction() ).toEqual(false);
})

Upvotes: 1

Related Questions