I’m trying to write some tests using mike:mocha. See below. I expected that the Account.createUser would affect only a testing database, and not my local dev database. However that’s not what I’m finding. It looks like the test is running against my local database as I see the user in my local database. Further when I run the test a second time, then it fails because the database begins with two users, not one.
Is this expected? Is there any way to run tests without altering my dev (or production) database?
if MochaWeb?
MochaWeb.testOnly ->
describe "Server initialization", ->
it "should have a Meteor version defined", ->
chai.assert Meteor.release
it "should have one user", ->
chai.assert.equal Meteor.users.find().count(), 1
it "should have one superuser", ->
chai.assert(BOSS.isSuperUser(Meteor.users.findOne()))
it "should allow user creation", ->
Accounts.createUser
username: "foo"
email: "foo@example.com"
chai.assert.equal 2, Meteor.users.find().count()
#chai.assert Meteor.user.findOne({username:'foo'})
Mocha does not spin up its own database. For dev, one way would be to start meteor with a clean db and create some fake (test) data at startup. Or you tell meteor to start with a different db for testing like here http://stackoverflow.com/questions/14166436/how-to-set-up-separate-test-and-development-database-in-meteor
I found this in the velocity readme:
“Mirrors are used by test frameworks to run tests within. Tests are typically destructive and as such require a different database. Mirrors run a parallel version of your app with a different database as not to intrude on the main development workflow.”
So it looks like the intent is for velocity/mocha to not use/modify the dev database. Is this functionality unimplemented or broken?
Actually, I think the problem is a little different.
-
Velocity Mocha does appear to create a mirror for testing, so I believe the test is not modifying the dev database.
-
The mocha mirror is not cleared between test runs.
-
meteor shell appears to connect to the mocha mirror process, not the dev process.
The last point is why I was thinking that the dev database was modified, as I was seeing the mocha mirror database, not the dev database, when I used meteor shell. Any ideas on how to get meteor shell to connect to the dev process?
alright, thanks for clarifying, seems I had the same misconception
Actually, I found this script that can be used to clear the database before any tests:
https://github.com/Sanjo/meteor-jasmine/wiki/Database-Fixtures-for-Integration-Tests
This one is in the Jasmine repository, but it seems to work for Mocha as well.