Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  55] [ 5]  / answers: 1 / hits: 111692  / 12 Years ago, fri, november 30, 2012, 12:00:00

According to the Jasmine documentation, a mock can be created like this:



jasmine.createSpyObj(someObject, ['method1', 'method2', ... ]);


How do you stub one of these methods? For example, if you want to test what happens when a method throws an exception, how would you do that?


More From » jasmine

 Answers
45

You have to chain method1, method2 as EricG commented, but not with andCallThrough() (or and.callThrough() in version 2.0). It will delegate to real implementation.



In this case you need to chain with and.callFake() and pass the function you want to be called (can throw exception or whatever you want):



var someObject = jasmine.createSpyObj('someObject', [ 'method1', 'method2' ]);
someObject.method1.and.callFake(function() {
throw 'an-exception';
});


And then you can verify:



expect(yourFncCallingMethod1).toThrow('an-exception');

[#81697] Thursday, November 29, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alyssiat

Total Points: 608
Total Questions: 102
Total Answers: 101

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
alyssiat questions
;