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

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() { ...@@ -426,70 +426,110 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
describe('set', function() { describe('set', function() {
it("should be configurable with global functions", function() { it("should be configurable with global functions", function() {
var overrideFunction = function() { var defaultClassMethod = sinon.spy()
return 'override'; , 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 = { this.sequelize.options.define = {
'classMethods': { 'classMethods': {
'defaultClassMethod': function() {}, 'defaultClassMethod': defaultClassMethod,
'overrideClassMethod': function() {} 'overrideClassMethod': overrideClassMethod
}, },
'instanceMethods': { 'instanceMethods': {
'defaultInstanceMethod': function() {}, 'defaultInstanceMethod': defaultInstanceMethod,
'overrideInstanceMethod': function() {} 'overrideInstanceMethod': overrideInstanceMethod
}, },
'setterMethods': { 'setterMethods': {
'default': function() {}, 'default': defaultSetterMethod,
'override': function() {}, 'override': overrideSetterMethod
}, },
'getterMethods': { 'getterMethods': {
'default': function() { 'default': defaultGetterMethod,
return 'default'; 'override': overrideGetterMethod
},
'override': function() {
return 'default';
}
} }
}; };
var testEntity = this.sequelize.define('TestEntity', {}, { var testEntity = this.sequelize.define('TestEntity', {}, {
'classMethods': { 'classMethods': {
'customClassMethod': function() {}, 'customClassMethod': customClassMethod,
'overrideClassMethod': overrideFunction 'overrideClassMethod': customOverrideClassMethod
}, },
'instanceMethods': { 'instanceMethods': {
'customInstanceMethod': function() {}, 'customInstanceMethod': customInstanceMethod,
'overrideInstanceMethod': overrideFunction 'overrideInstanceMethod': customOverrideInstanceMethod
}, },
'setterMethods': { 'setterMethods': {
'custom': function() {}, 'custom': customSetterMethod,
'override': function() {} 'override': customOverrideSetterMethod
}, },
'getterMethods': { 'getterMethods': {
'custom': function() { 'custom': customGetterMethod,
return 'custom'; 'override': customOverrideGetterMethod
},
'override': function() {
return 'custom';
}
} }
}); });
// 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(); var instance = testEntity.build();
expect(testEntity).to.have.property('defaultClassMethod'); // Call all Instance Methods
expect(testEntity).to.have.property('customClassMethod'); instance.defaultInstanceMethod();
expect(testEntity).to.have.property('overrideClassMethod'); instance.customInstanceMethod();
expect(testEntity.overrideClassMethod()).to.be.eql('override'); instance.overrideInstanceMethod();
expect(instance).to.have.property('defaultInstanceMethod');
expect(instance).to.have.property('customInstanceMethod'); expect(typeof instance.defaultInstanceMethod).to.equal('function');
expect(instance).to.have.property('overrideInstanceMethod'); expect(typeof instance.customInstanceMethod).to.equal('function');
expect(instance.overrideInstanceMethod()).to.be.eql('override'); expect(typeof instance.overrideInstanceMethod).to.equal('function');
expect(instance.default).to.be.eql('default');
expect(instance.custom).to.be.eql('custom'); expect(defaultInstanceMethod).to.have.been.calledOnce;
expect(instance.override).to.be.eql('custom'); expect(customInstanceMethod).to.have.been.calledOnce;
expect(overrideInstanceMethod.callCount).to.be.eql(0);
// @TODO: How to Test Setters? 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!