【发布时间】:2014-02-10 03:12:15
【问题描述】:
我的架构是:
var ItemSchema = new Schema({
sku: {
type: String,
trim: true,
index: true,
required: true
},
description: {
type: String,
trim: true,
required: true
},
client_id: {
type: Schema.ObjectId,
ref: 'Client',
index: true,
required: true
}
}, {versionKey: false, autoIndex: false});
ItemSchema.index({sku: 1, client_id: 1}, {unique: true});
我希望每个 client_id 的 sku 都是唯一的。所以我认为索引可以解决问题。我正在运行mocha 单元测试,测试是:
it('should fail if the sku is not unique per client', function(done) {
var secondItem = validItem;
return validItem.save(function(err) {
should.not.exist(err);
return secondItem.save(function(err) {
should.exist(err);
done();
});
});
});
保存第二个项目(相同的sku 和相同的client_id)应该会导致错误。但是,我没有收到任何错误:
1) <Unit Test> Model Item: Method Save should fail if the sku is not unique per client:
Uncaught AssertionError: expected null to exist
我做错了什么?
【问题讨论】:
-
你能试试这个吗? var secondItem = JSON.parse(JSON.Stringify(validItem));我的猜测是您的原始参考在
validItem.save之后失效