不要怂,就是干,撸起袖子干!

Commit d999464e by Jonatan Männchen

Testing with spies

1 parent d85e1803
Showing with 80 additions and 40 deletions
......@@ -426,70 +426,110 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
describe('set', function() {
it("should be configurable with global functions", function() {
var overrideFunction = function() {
return 'override';
};
var defaultClassMethod = sinon.spy()
, overrideClassMethod = sinon.spy()
, defaultInstanceMethod = sinon.spy()
, overrideInstanceMethod = sinon.spy()
, defaultSetterMethod = sinon.spy()
, overrideSetterMethod = sinon.spy()
, defaultGetterMethod = sinon.spy()
, overrideGetterMethod = sinon.spy()
, customClassMethod = sinon.spy()
, customOverrideClassMethod = sinon.spy()
, customInstanceMethod = sinon.spy()
, customOverrideInstanceMethod = sinon.spy()
, customSetterMethod = sinon.spy()
, customOverrideSetterMethod = sinon.spy()
, customGetterMethod = sinon.spy()
, customOverrideGetterMethod = sinon.spy();
this.sequelize.options.define = {
'classMethods': {
'defaultClassMethod': function() {},
'overrideClassMethod': function() {}
'defaultClassMethod': defaultClassMethod,
'overrideClassMethod': overrideClassMethod
},
'instanceMethods': {
'defaultInstanceMethod': function() {},
'overrideInstanceMethod': function() {}
'defaultInstanceMethod': defaultInstanceMethod,
'overrideInstanceMethod': overrideInstanceMethod
},
'setterMethods': {
'default': function() {},
'override': function() {},
'default': defaultSetterMethod,
'override': overrideSetterMethod
},
'getterMethods': {
'default': function() {
return 'default';
},
'override': function() {
return 'default';
}
'default': defaultGetterMethod,
'override': overrideGetterMethod
}
};
var testEntity = this.sequelize.define('TestEntity', {}, {
'classMethods': {
'customClassMethod': function() {},
'overrideClassMethod': overrideFunction
'customClassMethod': customClassMethod,
'overrideClassMethod': customOverrideClassMethod
},
'instanceMethods': {
'customInstanceMethod': function() {},
'overrideInstanceMethod': overrideFunction
'customInstanceMethod': customInstanceMethod,
'overrideInstanceMethod': customOverrideInstanceMethod
},
'setterMethods': {
'custom': function() {},
'override': function() {}
'custom': customSetterMethod,
'override': customOverrideSetterMethod
},
'getterMethods': {
'custom': function() {
return 'custom';
},
'override': function() {
return 'custom';
}
'custom': customGetterMethod,
'override': customOverrideGetterMethod
}
});
// Call all Class Methods
testEntity.defaultClassMethod();
testEntity.customClassMethod();
testEntity.overrideClassMethod();
expect(typeof testEntity.defaultClassMethod).to.equal('function');
expect(typeof testEntity.customClassMethod).to.equal('function');
expect(typeof testEntity.overrideClassMethod).to.equal('function');
expect(defaultClassMethod).to.have.been.calledOnce;
expect(customClassMethod).to.have.been.calledOnce;
expect(overrideClassMethod.callCount).to.be.eql(0);
expect(customOverrideClassMethod).to.have.been.calledOnce;
// Create Instance to test
var instance = testEntity.build();
expect(testEntity).to.have.property('defaultClassMethod');
expect(testEntity).to.have.property('customClassMethod');
expect(testEntity).to.have.property('overrideClassMethod');
expect(testEntity.overrideClassMethod()).to.be.eql('override');
expect(instance).to.have.property('defaultInstanceMethod');
expect(instance).to.have.property('customInstanceMethod');
expect(instance).to.have.property('overrideInstanceMethod');
expect(instance.overrideInstanceMethod()).to.be.eql('override');
expect(instance.default).to.be.eql('default');
expect(instance.custom).to.be.eql('custom');
expect(instance.override).to.be.eql('custom');
// @TODO: How to Test Setters?
// Call all Instance Methods
instance.defaultInstanceMethod();
instance.customInstanceMethod();
instance.overrideInstanceMethod();
expect(typeof instance.defaultInstanceMethod).to.equal('function');
expect(typeof instance.customInstanceMethod).to.equal('function');
expect(typeof instance.overrideInstanceMethod).to.equal('function');
expect(defaultInstanceMethod).to.have.been.calledOnce;
expect(customInstanceMethod).to.have.been.calledOnce;
expect(overrideInstanceMethod.callCount).to.be.eql(0);
expect(customOverrideInstanceMethod).to.have.been.calledOnce;
// Call Getters
var defaultVal = instance.default
, custom = instance.custom
, override = instance.override;
expect(defaultGetterMethod).to.have.been.calledOnce;
expect(customGetterMethod).to.have.been.calledOnce;
expect(overrideGetterMethod.callCount).to.be.eql(0);
expect(customOverrideGetterMethod).to.have.been.calledOnce;
// Call Setters
instance.default = 'test';
instance.custom = 'test';
instance.override = 'test';
expect(defaultSetterMethod).to.have.been.calledOnce;
expect(customSetterMethod).to.have.been.calledOnce;
expect(overrideSetterMethod.callCount).to.be.eql(0);
expect(customOverrideSetterMethod).to.have.been.calledOnce;
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!