Mocha在稍后创建时会从MongoDb中查找用户

时间:2017-04-01 09:51:21

标签: javascript node.js mongodb mocha

我试图找出如何在此代码的开头从数据库中找到用户,而稍后在代码中创建用户。

Mocha文档说

  

测试可以在你的钩子之前,之后或穿插中出现。钩子将按其定义的顺序运行,视情况而定;所有before()挂钩运行(一次),然后任何beforeEach()挂钩,测试,任何afterEach()挂钩,最后挂钩()挂钩(一次)。

var assert = require('assert');
var express = require('express');
var status = require('http-status');
var superagent = require('superagent');
var wagner = require('wagner-core');

var URL_ROOT = 'http://localhost:3000';
var PRODUCT_ID = '000000000000000000000001';

describe('User API', function() {
  var server;
  var Category;
  var Product;
  var User;

  before(function() {
    var app = express();

    models = require('./models')(wagner);

    Category = models.Category;
    Product = models.Product;
    User = models.User;

    app.use(function(req, res, next) {
      User.findOne({}, function(error, user) {
        assert.ifError(error);

        req.user = user;
        console.log(user);
        next();
      });
    });

    app.use(require('./api')(wagner));

    server = app.listen(3000);
  });

  after(function() {
    server.close;
  });

  beforeEach(function(done) {
    Category.remove({}, function(error) {
      assert.ifError(error);
      Product.remove({}, function(error) {
        assert.ifError(error);
        User.remove({}, function(error) {
          assert.ifError(error);
          done();
        });
      });
    });
  });

  beforeEach(function(done) {
    var categories = [
      { _id: 'Electronics' },
      { _id: 'Phones', parent: 'Electronics' },
      { _id: 'Laptops', parent: 'Electronics' },
      { _id: 'Bacon' }
    ];

    var products = [
      {
        name: 'LG G4',
        category: { _id: 'Phones', ancestors: ['Electronics', 'Phones'] },
        price: {
          amount: 300,
          currency: 'USD'
        }
      },
      {
        _id: PRODUCT_ID,
        name: 'Asus Zenbook Prime',
        category: { _id: 'Laptops', ancestors: ['Electronics', 'Laptops'] },
        price: {
          amount: 2000,
          currency: 'USD'
        }
      },
      {
        name: 'Flying Pigs Farm Pasture Raised Pork Bacon',
        category: { _id: 'Bacon', ancestors: ['Bacon'] },
        price: {
          amount: 20,
          currency: 'USD'
        }
      }
    ];

    var users = [{
      profile: {
        username: 'vkarpov15',
        picture: 'http://pbs.twimg.com/profile_images/550304223036854272/Wwmwuh2t.png'
      },
      data: {
        oauth: 'invalid',
        cart: []
      }
    }];
    Category.create(categories, function(error) {
      assert.ifError(error);
      Product.create(products, function(error) {
        assert.ifError(error);
        User.create(users, function(error) {
          assert.ifError(error);
          done();
        });
      });
    });
  });

  it('can save users cart', function(done) {
    var url = URL_ROOT + '/me/cart';

    superagent.
      put(url).
      send({
        data: {
          cart: [{ product: PRODUCT_ID, quantity: 1 }]
        }
      }).
      end(function(error,res) {

        assert.ifError(error);
        assert.equal(res.status, status.OK);
        User.findOne({}, function(error, user) {

          assert.ifError(error);
          assert.equal(user.data.cart.length, 1);
          assert.equal(user.data.cart[0].product, PRODUCT_ID);
          assert.equal(user.data.cart[0].quantity, 1);
          done();
        });
      });
  });

})

如何在此处找到用户'vkarpov15',而用户是在稍后出现的beforeEach函数中创建的。

app.use(function(req, res, next) {
      User.findOne({}, function(error, user) {
        assert.ifError(error);

        req.user = user;
        console.log(user);
        next();
      });
    });

它在此处创建,稍后在代码中创建:

Category.create(categories, function(error) {
      assert.ifError(error);
      Product.create(products, function(error) {
        assert.ifError(error);
        User.create(users, function(error) {
          assert.ifError(error);
          done();
        });
      });
    });

1 个答案:

答案 0 :(得分:1)

在那里没有查询用户,该代码只注册了一个快速中间件,它将在每次向服务器发出请求之前查询用户。如您所见,请求是在it测试用例中发出的,但是在每个测试用例之前都会创建一个用户,请参阅beforeEach块。

ps:你错过了后块server.close()之后的括号。