I am trying to define a simple Mongoose model for a learning ExpressJS app.
# 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)
I have a Todos
class that is meant for other classes to use. (Not sure if its better to just use the Todo
model tho?)
# Todos.coffee
Todo = require "Todo"
class Todos
byId: (id, callback) ->
Todo.findOne { _id: id }, callback
Then in my Mocha/should unit test ...
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()
But I am getting ...
$ 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)
What did I do wrong?
In Todos.coffee
, your require
of Todo
needs to include a relative path so that it is looked for as a local file and not an installed node module.
Todo = require "./Todo"