S
S
Sergey2015-04-10 11:30:21
JavaScript
Sergey, 2015-04-10 11:30:21

How to test functionality using Mocha?

I want to write the first test for the Mocha article comment system.
What is the testing methodology?
Scheme

var CommentSchema = new Schema({
  created: {
    type: Date,
    default: Date.now
  },
  content: {
    type: String,
    default: '',
    trim: true,
                required: 'Content cannot be blank'
    
  },
  articleId: {
    type: Schema.ObjectId,
    ref: 'Article'
  },
  
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  }
});

I want to check that a comment cannot be left unlogged and that the comment belongs to the same article.
What should I do?
1) beforeEach
user = new User({
            firstName: 'Full',
            lastName: 'Name',
            displayName: 'Full Name',
            email: '[email protected]',
            username: credentials.username,
            password: credentials.password,
            provider: 'local'
        });

           user.save(function () {
            article = {
                title: 'Article Title',
                content: 'Article Content'
            };
            comment = {
                content: 'Article Comment',
                articleID: article,
                user: user
            };
            done();
        });

I need to save the article somehow first and then save the comment with artilceId: (how to get the id of the article) and then somehow compare the id of the recorded article and the articleId in the comment? I can't put it together in my head

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-04-10
@k12th

See how it's done in node-express-mongoose-demo tests . True, there are rather integration tests, and not a unit, but the meaning is the same and approximately the same is checked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question