admin管理员组

文章数量:1426626

I'm trying to test if the String.toUpperCase method is called with Jasmine. However whenever I try it just returns

toUpperCase() method does not exist

Here is my Jasmine test:

spyOn(String,"toUpperCase")
$(@makeup.el).trigger(@e)
expect(String.toUpperCase).toHaveBeenCalled()

Any ideas on how to test if that is called? It appears that String is a private class to the window object, and so I may not actually be able to test this. Please help.

I'm trying to test if the String.toUpperCase method is called with Jasmine. However whenever I try it just returns

toUpperCase() method does not exist

Here is my Jasmine test:

spyOn(String,"toUpperCase")
$(@makeup.el).trigger(@e)
expect(String.toUpperCase).toHaveBeenCalled()

Any ideas on how to test if that is called? It appears that String is a private class to the window object, and so I may not actually be able to test this. Please help.

Share Improve this question edited Jan 5, 2017 at 9:57 danwellman 9,4038 gold badges63 silver badges92 bronze badges asked Jul 11, 2013 at 0:09 Blaine KastenBlaine Kasten 1,7031 gold badge16 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

There is no String.toUpperCase function. There is, however, a String.prototype.toUpperCase function which is what "pancakes".toUpperCase() will use. You should have better luck with:

spyOn(String.prototype, 'toUpperCase')
#...
expect(String.prototype.toUpperCase).toHaveBeenCalled()

However, native functions aren't guaranteed to behave like functions that are implemented in JavaScript so don't be surprised if this doesn't work either.

Checking that the toUpperCase method has been called anywhere (which is what wrapping String.prototype.toUpperCase with a spy will do) seems a bit pointless since strings are used all over the place; spying on a particular string would make more sense but even then, this particular test still seems a bit pointless.

本文标签: javascriptTesting toUpperCase with JasmineStack Overflow