Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD

Thanks to Danyal Aytekin for his Stackoverflow answer. If you want to use the toThrow in Jasmin tests on actual functions, you can use the “Function.bind, which was introduced in JavaScript 1.8.5. This works in the latest versions of Chrome and Firefox, and you can patch other browsers by defining the function yourself.”

From his answer:

describe('using bind with jasmine', function() {
    var f = function(x) {
        if(x === 2) {
            throw new Error();
        }
    }

    it('lets us avoid using an anonymous function', function() {
        expect(f.bind(null, 2)).toThrow();
    });
});
 
Thanks Danyal!
EDIT: An alternative to this is to use the try catch:
describe('with try catch', function() {
    var f = function(x) {
        if(x === 2) {
            throw new Error();
        }
    }

    it('lets us avoid using an anonymous function', function() {
       try {
           f(2);
           // fail if we get here, the exception wasn't thrown
           expect(false).toBeTruthy();
       }
       catch(){
           expect(true).toBeTruthy();
    });
});
Posted on Thursday, March 21, 2013 2:11 PM Unit Testing , JavaScript , Jasmine | Back to top


Comments on this post: Use the bind method for Jasmine toThrow Tests

# re: Use the bind method for Jasmine toThrow Tests
Requesting Gravatar...
Good article!
Hope useful for all!

Thanks
Left by souvenir pernikahan on Mar 21, 2013 9:01 PM

Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net