我正在尝试为学习ExpressJS应用程序定义一个简单的Mongoose模型。
# Todo.coffee
mongoose = require "mongoose"
Schema = mongoose.Schema
todoSchema = new Schema
title: String
desc: String
dueOn: Date
completedOn: Date
Todo = new mongoose.model("Todo", todoSchema)
我有一个Todos
类,供其他类使用。 (不确定是否更好地使用Todo
模型?)
# Todos.coffee
Todo = require "Todo"
class Todos
byId: (id, callback) ->
Todo.findOne { _id: id }, callback
然后在我的Mocha / should单元测试......
require "should"
Todos = require "../../../app/todos/Todos.coffee"
Todo = require "../../../app/todos/Todo.coffee"
describe "Todos", ->
describe "byId", ->
it "should return Todo with given ID", (done) ->
Todos.byId 1, (err, todo) ->
todo.should.be.an.instanceOf(Todo)
done()
但我得到......
$ mocha --compilers coffee:coffee-script tests/backend/todos/TodosTests.coffee
module.js:340
throw err;
^
Error: Cannot find module 'Todo'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/labs/Projects/Tickle/app/todos/Todos.coffee:4:10)
at Object.<anonymous> (/labs/Projects/Tickle/app/todos/Todos.coffee:20:4)
at Module._compile (module.js:449:26)
at Object.require.extensions..coffee (/usr/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:22:21)
我做错了什么?
答案 0 :(得分:4)
在Todos.coffee
中,require
Todo
Todo = require "./Todo"
需要包含相对路径,以便将其作为本地文件而不是已安装的节点模块进行查找。
{{1}}