Skip to content

Bad tests in default User model spec #804

@dd-dent

Description

@dd-dent

Consider the following excerpt ( from server/api/user/user.model.spec.js:35-43 ):

  it('should fail when saving a duplicate user', function(done) {
    user.save(function() {
      var userDup = new User(user);
      userDup.save(function(err) {
        should.exist(err);
        done();
      });
    });
  });

And here's the error returned from the second, duplicate save:

{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: sample-test.users.$_id_  dup key: { : ObjectId('54ca85ef65b199ea1ebbbd67') }]
  name: 'MongoError',
  code: 11000,
  err: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: sample-test.users.$_id_  dup key: { : ObjectId(\'54ca85ef65b199ea1ebbbd67\') }' }

In short, an error is returned when the test is run, but not due to any validation error (as expected),
but due to a duplicate id (I.E. user.id === dupUser.id)

To solve, I created a plain object containing the user data, Used the Model.create method to create the user in test and to make sure what ought to be tested was tested, I compared the returned error message to what was defined in the validation:

  it('should fail when saving a duplicate user', function(done) {
    User.create(data, function() {
      User.create(data, function(err) {
        should.equal(err.errors.email.message, 'The specified email address is already in use.');
        done();
      });
    });
  });

I'm not sure how crucial this is, as the actual validation is sound, but I don't suppose it's good to leave it as it is...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions