私はこれをプロダクションコードに持っています:
$(document).ready ->
if $( "#accordion" ).length > 0
$( "#accordion" ).accordion()
どちらが機能するか、テストしたいと思います:
describe "Non-MVCd stuff", ->
beforeEach ->
spyOn( $('#accordion').prototype, 'accordion' )
if $("#accordion").length > 0
$("#accordion").accordion()
describe "accordion", ->
it 'works', ->
expect( $('#accordion' ).length ).toBe( 1, 'the element must be there.' )
expect( $('#accordion').prototype.accordion ).toHaveBeenCalled()
次のエラーが表示されます。
spyOn could not find an object to spy upon for accordion()
TypeError: $(...).prototype is undefined in http://pi.local:3011/assets/spec/views/utils_spec.js?body=1 (line 14)
プロトタイプがないと動作しません (そして、 $("#accordion").prototype is undefined ):
describe "Non-MVCd stuff", ->
beforeEach ->
spyOn( $('#accordion'), 'accordion' )
if $("#accordion").length > 0
$("#accordion").accordion()
describe "accordion", ->
it 'works', ->
expect( $('#accordion' ).length ).toBe( 1, 'the element must be there.' )
expect( $('#accordion').accordion ).toHaveBeenCalled()
_
Error: Expected a spy, but got Function. in http://pi.local:3011/assets/jasmine-1.1.0/jasmine.js?body=1 (line 1286)