As I continue to learn Angular, my search for information has yielded minimal results. However, one resource that stood out was a post on Stack Overflow titled How to write a test which expects an Error to be thrown in Jasmine?
After reviewing the aforementioned post, I encountered an issue.
My current challenge involves unit testing a method that is expected to throw an error. The goal is to catch this error during testing to confirm its success. Here is the function call in question:
expect(function(){instance.selectMember(event);}).toThrow();
Below is the line where the error is generated:
throw new Error('member not found: ' + member.id);
The outcome of this test scenario was as follows:
Expected function to throw an Error.
This was followed by:
Failed: member not found: 42
Error: member not found: 42
Despite displaying the error message indicating failure, it did not actually throw an error. This left me puzzled.
I attempted alternative methods such as:
expect(function(){instance.selectMember(event);}).toThrow(new Error('member not found: 42'));
And also tried:
expect(function(){instance.selectMember(event);}).toThrowError('member not found: 42');
However, these attempts yielded identical results.
How can I verify that my unit test successfully triggers the error as intended?