Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit Test with Mongoose
    text
    copied!<p>I'm new to Node.js, Mongoose, and testing in this environment. I have the following schema declared in a separate file. </p> <pre><code>Issue = mongoose.model("Issue", { identifier: String, date: String, url: String, name: String, thumbnailURL: String }); </code></pre> <p>Then I have this method which simply returns all of the <code>Issue</code> instances in the MongoDB collection. </p> <pre><code>function issues(request, response) { response.setHeader('Content-Type', 'text/json'); Issue.find().sort('date').exec(function(error, items) { if (error) { response.send(403, {"status": "error", "error:": exception}); } else { response.send(200, {"issues": items}); } }); } </code></pre> <p>I've gotten this far through experimentation, and now I want to test it, but I've run into a problem. How do I go about testing it, without setting up a MongoDB connection, etc. I know that I <em>can</em> set all that stuff up, but that's an integration test. I want to write unit tests to test things like:</p> <ul> <li>Does the function set the content type correctly</li> <li>Does the function sort by the <code>date</code> field</li> <li>Does the function return a 403 when an error occurs?</li> <li>... and so on</li> </ul> <p>I'm curious to see how I could refactor my existing code to make it more unit testable. I've tried maybe creating a second function that's called through, accepting the <code>response</code> and <code>Item</code> schema objects as parameters, but it doesn't feel right. Anyone have any better suggestions? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload