mercredi 26 septembre 2018

JavaScript Jasmine Framework how to write a Test to Fail case

I have done testing of software with 'Test to pass' cases and 'Test to fail'. Test to fail cases are very important for developing good software as that is where most of the bugs come from. I am wondering how I can write these kinds of unit tests using the Jasmine framework?

Example of a Test to Pass:

it('should set position to true', () => {
  spyOn(service, 'SetPositionAsPositive');
  service.SetPositionAsPositive();
  expect(service.position).toEqual(true);
});

Not A proper Test to Fail case would be something like this:

it('should not set position to false', () => {
  spyOn(service, 'SetPositionAsPositive');
  service.SetPositionAsPositive();
  expect(service.position).toEqual(false);
});

However as expected this fails my test case instead of passing which is correct. But I would like the test case to expect to fail not to pass. So a pass would be failure for these kinds of test cases.

The best I have come up with is this below however I think this still counts as a test to pass?

it('should not set position to false', () => {
  spyOn(service, 'SetPositionAsPositive');
  service.SetPositionAsPositive();
  expect(service.position).not.toEqual(false);
});

Aucun commentaire:

Enregistrer un commentaire