我正在尝试测试 .js.coffee 文件,但我不断收到错误:
Failure/Error: TypeError: undefined is not a constructor (evaluating 'Number.isNaN(likesCount)')
我相信我需要在我的规范文件中包含 Number,我该怎么做?我是规范测试的新手。
这是我的 js.coffee 文件
class Page.LikeButtons
constructor: (@el) ->
Page.handleAjaxOn @el.find('.js-create-like'), (event, data) =>
@_increaseCountOnPage()
_increaseCountOnPage: () ->
likesContent = @el.find('button.like-count.js-show-modal')
likesElement = likesContent[0].innerHTML.split(' ')
likesCount = parseInt(likesElement[0])
like = "Likes"
if (Number.isNaN(likesCount))
like = "Like";
likesCount = 0;
likesContent[0].innerHTML = "#{parseInt(likesCount) + 1 } #{like}"
$(document).ready ->
for el in $('.js-like-button-container')
new Page.LikeButtons($(el))
这是我的 spec.js.coffee 文件
describe 'like buttons', ->
beforeEach ->
setFixtures '''
<div class="js-like-button-container" data-taste-id="456">
<div class="hidden js-hideable-modal"></div>
<button class="like-count js-show-modal"></button>
<div class="js-create-like"></div>
<div class="js-delete-like"></div>
</div>
'''
new Page.LikeButtons($('.js-like-button-container'))
describe 'when the user clicks to like', ->
beforeEach ->
$('.js-create-like').trigger('ajax:success', [{like: {id: 123}}])
it 'classes the container as current-likes', ->
expect($('.js-like-button-container')).toHaveClass 'current-likes'
it 'builds an href for the delete button for the created like', ->
expect($('.js-delete-like').attr('href')).toEqual(
'/api/...'
)