Mocha测试开始失败进行GET调用

时间:2013-11-29 20:21:31

标签: node.js express mocha

升级到Node 0.10和Express 3后,我的mocha测试开始失败。即使相同资源上的PUT和POST调用正常工作,每个GET调用也会失败并显示404。单元测试没有改变,我已经使用浏览器验证了端点。只有在摩卡的背景下,他们才能以404失败。

这是一个示例测试,

var user = new UserModel({
    email: 'mocha@example.com',
    pass: 'password',
    dob: Date.now(),
    role: 'patient'
});

var deviceid = null;
var notifications;

describe('Device Token API',function() {
    before(function(done) {
        mongoose.connect('mongodb://localhost/dev', function(err) {
            UserModel.remove({email:'mocha@example.com'},function(err){
                done(err);
            });
        });
    });
    describe('POST /api/users', function() {
        it('should allow creation of users', function(done) {
            request(app).post("/api/users")
            .send({password:'password',
                  confirm_password:'password',
                  email:'mocha@example.com',
                  fnam:'Mocha',
                  lnam:'Dude',
                  dob:new Date(1970,1,1)
            })
            .expect(201)
            .end(function(err,res) {
                user = res.body;
                done();
            });
        });
    });
    describe('POST /api/sign-in', function() {
        it('should allow user to sign-in', function(done) {
            request(app).post("/api/sign-in")
            .send( { email: 'mocha@example.com', password: 'password' })
            .expect(200)
            .end(function(err,res) {
                cookie = res.headers['set-cookie'];
                done();
            });
        });
    });
describe('POST /api/users/:userid/devicetokens', function() {
    it('should respond with 201 and id', function(done) {
        request(app).post("/api/users/" + user._id + "/devicetokens")
        .set('cookie',cookie)
        .send({
            token:'IOSTOKENFORPUSHNOTIFICATION',
            device: 'ios',
            device_name: 'mocha man phone'
        })
        .expect(201,function(err,res) {
            deviceid = res.body.id;
            done();
        });
    });
});
describe('PUT /api/users/:userid/devicetokens/:id', function() {
    it('should respond with 204', function(done) {
        request(app).put("/api/users/" + user._id + "/devicetokens/" + deviceid)
        .set('cookie',cookie)
        .send({
            token:'IOSTOKENFORPUSHNOTIFICATION2',
            device: 'ios',
            device_name: 'mocha man phone'
        })
        .expect(204,done);
    });

    it('should not allow mac address modification', function(done) {
        request(app).put("/api/users/" + user._id + "/devicetokens/" + deviceid)
        .set('cookie',cookie)
        .send({
            token:'IOSTOKENFORPUSHNOTIFICATION3',
            device: 'ios',
            device_name: 'mocha ipad'
        })
        .expect(500,done);
    });
});
    describe('GET /api/users/:userid/devicetokens', function() {
        it('should respond with a single device token', function(done) {
            request(app).get("/api/users/" + user._id + "/devicetokens/")
            .set('cookie',cookie)
            .expect(200)
            .end(function(err,res) {
                console.dir(res.body);
                res.body.should.have.length(1);
                res.body[0].token.should.equal('IOSTOKENFORPUSHNOTIFICATION2');
                res.body[0].device.should.equal('ios');
                res.body[0].device_name.should.equal('mocha man phone');
                done();
            });
        });
    });
});

我使用的中间件都没有来自摩卡,所以404必须埋在Express的某个地方。此示例中未显示PUT和POST调用,它们正常工作,因此我确信路由定义正在起作用。

以下是Mocha结果,

HOST=local TZ=UTC REDIS_ENV=on ./node_modules/.bin/mocha --bail --ignore-leaks --reporter spec --timeout 10s test/devicetokens.api.js
INFO - Fri, 29 Nov 2013 20:35:03 GMT - Host name is localhost:8080
INFO - Fri, 29 Nov 2013 20:35:03 GMT - server started on port 8080


  Device Token API
    POST /api/users
      ◦ should allow creation of users: INFO - Fri, 29 Nov 2013 20:35:03 GMT - ... sign-up in group undefined
INFO - Fri, 29 Nov 2013 20:35:03 GMT - New user creation:
INFO - Fri, 29 Nov 2013 20:35:03 GMT - ... email sent
INFO - Fri, 29 Nov 2013 20:35:03 GMT -  to: mocha@example.com
      ✓ should allow creation of users (130ms)
INFO - Fri, 29 Nov 2013 20:35:03 GMT - 127.0.0.1 - POST /api/users 201 557 - 125 ms
    POST /api/sign-in
      ✓ should allow user to sign-in 
INFO - Fri, 29 Nov 2013 20:35:03 GMT - 127.0.0.1 - POST /api/sign-in 200 557 - 11 ms
    POST /api/users/:userid/devicetokens
      ◦ should respond with 201 and id: DEBUG - Fri, 29 Nov 2013 20:35:03 GMT - {
    token: 'IOSTOKENFORPUSHNOTIFICATION',
    device: 'ios',
    device_name: 'mocha man phone'
}
      ✓ should respond with 201 and id 
INFO - Fri, 29 Nov 2013 20:35:03 GMT - 127.0.0.1 - POST /api/users/5298fa7741fb79cbbf000019/devicetokens 201 38 - 22 ms
    PUT /api/users/:userid/devicetokens/:id
      ◦ should respond with 204: INFO - Fri, 29 Nov 2013 20:35:03 GMT - 127.0.0.1 - PUT /api/users/5298fa7741fb79cbbf000019/devicetokens/5298fa7741fb79cbbf000031 204 - - 7 ms
      ✓ should respond with 204 
      ✓ should not allow mac address modification 
INFO - Fri, 29 Nov 2013 20:35:03 GMT - 127.0.0.1 - PUT /api/users/5298fa7741fb79cbbf000019/devicetokens/5298fa7741fb79cbbf000031 500 4 - 8 ms
    GET /api/users/:userid/devicetokens
      ◦ should respond with a single device token: INFO - Fri, 29 Nov 2013 20:35:03 GMT - 127.0.0.1 - GET /api/users/5298fa7741fb79cbbf000019/devicetokens/ 404 - - 3 ms
{}
ERROR - Fri, 29 Nov 2013 20:35:03 GMT - Caught Error AssertionError: expected {} to have a property 'length'

0 个答案:

没有答案