简体   繁体   中英

How do I run tests for a single model using mocha/mean.js?

I've been looking into mean.js testing. There's a grunt test task that runs all app with a single and easy grunt test , but I don't want to run all tests everytime.

I've installed mocha globally, and want to run tests for a single model using it, but doing...

mocha 'app/tests/mymodelname.server.model.js'

Throws me back a

"MissingSchemaError: Schema hasn't been registered for model MyModelName"

What would be the easiest way to run single tests for the server?

Thanks!

D

Previous replies are completely correct, there's a mongoose loader that is defined within grunt and passed into the 'grunt test' task. Take a look at gruntfile.js : grunt.registerTask('test', ['env:test', 'mongoose', 'mochaTest', 'karma:unit']);

I've looked around for nice solutions to this, and there should be a '--grep' switch to use with Mocha, but getting it passed in through grunt is beyond my current skillset - suggestions welcome!

I'm sure there's a really great way to do this, but for now I've just restricted the test file configuration list when wanting to work with just one test. I've modified ' config\\assets\\test.js ' to point to just one file. It's not pretty, and I'll probably get burned for suggesting it, but it works for me right now.

As robertklep mentioned, the best way to do this is by using the --require option in mocha to require server.js file (which includes all the required initialization for database module and additional stuff).

So in this case if you are running a mean.js app, the right way to do it would be:

mocha 'app/tests/mymodelname.server.model.js' -r server.js

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM