摩卡“$未定义”(Coffeescript)

时间:2014-06-12 15:32:38

标签: jquery unit-testing coffeescript mocha

我们刚刚开始使用Mocha。我们正在用coffeescript编写测试并测试我们的coffeescript文件。但是,我们遇到了一个小问题。这是问题......

../userLogin.coffee:12
  $(function() {
  ^
ReferenceError: $ is not defined

这是测试文件:

chai = require('chai')
chai.should()

userLogin = require('../../private/coffeescripts/users/userLogin.coffee').userLogin

describe 'User login', ->
    it 'return true with valid credentials', ->
        userLogin('test@example.com', 'password').should.be true
    it 'return false with invalid credentials',  ->
        userLogin('invalid', '').should.be false

这是我们用来运行测试的文件:

# DOM READY =========================================================================
$ ->
    $("button#submitButton").on "click", loginSubmit

# jQuery ================================================================================
# Submit via the enter button
$(document).keypress (event) ->
  if event.keyCode is 13
    loginSubmit()

# CoffeeScript Functions =================================================================
loginSubmit = ->
  emailInput = document.getElementById("emailInput").value
  passwordInput = document.getElementById("passwordInput").value

  # Check if either of the fields are empty
  if emailInput is "" or passwordInput is ""
    if emailInput is ""
      alert "Please fill in an email."
    else
      alert "Please fill in a password."

userLogin = (email, password)->
  true

root.userLogin = userLogin

任何帮助都会很棒。

0 个答案:

没有答案