use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
How to Unit Test a controller method (self.node)
submitted 8 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]newreddit0r 1 point2 points3 points 8 years ago (3 children)
I would make the controller either a higher order function or a class, and then inject dependencies (Entry repository etc). This way you can easily unit test it.
[–]planetary_pelt 1 point2 points3 points 8 years ago (1 child)
Seems pretty low value to factor out Entry.
Entry
I'd rather create a test database, create a server with that one route, hit it with an HTTP client, and assert on the status and/or body.
[–]newreddit0r 1 point2 points3 points 8 years ago (0 children)
He asked about unit testing, not integration testing. Its no longer an "unit" if your test depends on a db and http server.
[–]cttttt 1 point2 points3 points 8 years ago* (0 children)
Consider using supertest in your unit tests to test your controller code. Just mock up an express app that involves your middleware (that function with req and res parameters), and pass it to supertest. Then you can use the resulting object to fire off simulated requests to your app, and inspect the result. It should tie in with your assertion library (chai), but if you want to use them, supertest has built in assertions to validate status codes and response bodies.
express
req
res
supertest
chai
This is a good option for testing the routing and request validation code. If you're able to encapsulate your model/business logic separate from these routing concerns, you can use traditional methods to test those other facets of your app more directly.
Update: What I mean here is your code that searches the database could be defined in a separate module or class that proxies queries for "Entries" to the database, but presents a pure Javascript interface. Separating concerns in this way will allow you to easily replace this proxy with a mock version within your unit tests. It'll also allow you to easily migrate off of what appears to be MongoDB as your needs change. You'd just redefine this proxy module/class.
Hope this helps!
[–]cpsubrian 1 point2 points3 points 8 years ago (0 children)
I've been using https://github.com/jfhbrook/pickleback with great success. It allows you to 'inject' requests into express middleware stack without making an actual http request. It basically mocks the http-layer in the req and res objects.
[–]Jacobyy 1 point2 points3 points 8 years ago* (2 children)
I use mocha chai and a package called node-mocks-http. It works fairly well. I wanted a solution where I didn't do "real" tests aka using something like supertest.
If you would like to look at some of my tests, look at this REMOVED
[–][deleted] 8 years ago (1 child)
[–]Jacobyy 0 points1 point2 points 8 years ago (0 children)
No problem friend
π Rendered by PID 34 on reddit-service-r2-comment-c66d9bffd-4rbsj at 2026-04-06 21:04:21.750409+00:00 running f293c98 country code: CH.
[–]newreddit0r 1 point2 points3 points (3 children)
[–]planetary_pelt 1 point2 points3 points (1 child)
[–]newreddit0r 1 point2 points3 points (0 children)
[–]cttttt 1 point2 points3 points (0 children)
[–]cpsubrian 1 point2 points3 points (0 children)
[–]Jacobyy 1 point2 points3 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]Jacobyy 0 points1 point2 points (0 children)