简体   繁体   中英

Angular unit testing with parameter

I am writing a test for a method that takes in an id as a parameter to delete an object. The test case looks like below:

it('should delete the car object for the given ID', () => {
    const mockId = '1';
    const carObj = component.deleteCar(mockId);
    expect(carObj).not.toEqual('1');
  });

The test case passes. However when I change the value for assertion from '1' to '2', it still passes the test. Where am I going wrong ?

It will always pass because your assertion is not correct.

I'm not entirely sure what your deleteCar returns. Looking at name, i think its a type of Object . So, assuming it is returning an object, you are asserting that the object is not '1' in your code block above. Which will pass, because '1' (string type) is indeed not equal to object carObj . Similarly, the case when you assert expect(carObj).not.toEqual('2'); will also pass, because 2 !== CarObject.

There is not much information, but I think you would ideally perform an action eg click of button, and this will trigger the function deleteCar , rather than you calling manually, like in the case bove

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM