Conditions | 1 |
Paths | 1 |
Total Lines | 88 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var utilities = require('../src/utilities.js'); |
||
3 | describe('utilities.inherits', function() { |
||
4 | describe('failing tests', function() { |
||
5 | it('should fail because the Constructor is undefined', function() { |
||
6 | expect(utilities.inherits.bind()).toThrowError(TypeError); |
||
7 | }); |
||
8 | |||
9 | it('should fail because the Constructor is null', function() { |
||
10 | expect(utilities.inherits.bind(null, null)).toThrowError(TypeError); |
||
11 | }); |
||
12 | |||
13 | it('should fail because the SuperConstructor is undefined', function() { |
||
14 | expect(utilities.inherits.bind(null, function() {})).toThrowError( |
||
15 | TypeError |
||
16 | ); |
||
17 | }); |
||
18 | |||
19 | it('should fail because the SuperConstructor is null', function() { |
||
20 | expect(utilities.inherits.bind(null, function() {}, null)).toThrowError( |
||
21 | TypeError |
||
22 | ); |
||
23 | }); |
||
24 | |||
25 | it('should fail because the SuperConstructor has no prototype', function() { |
||
26 | expect(utilities.inherits.bind(null, function() {}, [])).toThrowError( |
||
27 | TypeError |
||
28 | ); |
||
29 | }); |
||
30 | }); |
||
31 | |||
32 | describe('successful tests', function() { |
||
33 | // test example from README.md |
||
34 | var SuperClass = function() { |
||
35 | this.someProperty = 42; |
||
36 | }; |
||
37 | |||
38 | SuperClass.prototype.retMsg = function(msg) { |
||
39 | return msg; |
||
40 | }; |
||
41 | |||
42 | SuperClass.prototype.getSomeProp = function() { |
||
43 | return this.someProperty; |
||
44 | }; |
||
45 | |||
46 | // a class we want to inherit from SuperClass |
||
47 | var SomeClass = function() { |
||
48 | SuperClass.call(this); |
||
49 | }; |
||
50 | |||
51 | utilities.inherits(SomeClass, SuperClass); |
||
52 | |||
53 | SomeClass.prototype.flop = function(msg) { |
||
54 | return this.retMsg(msg); |
||
55 | }; |
||
56 | |||
57 | var someClass_instance = new SomeClass(); |
||
58 | // end test example from README.md |
||
59 | |||
60 | it('should be able to access inherited properties', function() { |
||
61 | expect(someClass_instance.someProperty).toEqual(42); |
||
62 | }); |
||
63 | |||
64 | it('should be able to run inherited methods', function() { |
||
65 | expect(someClass_instance.retMsg('hi')).toEqual('hi'); |
||
66 | }); |
||
67 | |||
68 | it('should be able to run inherited methods by using the this keyword', function() { |
||
69 | expect(someClass_instance.flop('hi')).toEqual('hi'); |
||
70 | }); |
||
71 | |||
72 | it('should be instance of SomeClass', function() { |
||
73 | expect(someClass_instance instanceof SomeClass).toEqual(true); |
||
74 | }); |
||
75 | |||
76 | it('should be instance of SuperClass', function() { |
||
77 | expect(someClass_instance instanceof SuperClass).toEqual(true); |
||
78 | }); |
||
79 | |||
80 | it('should set SuperClass contructor', function() { |
||
81 | expect(someClass_instance.constructor.super_ === SuperClass).toEqual( |
||
82 | true |
||
83 | ); |
||
84 | }); |
||
85 | |||
86 | it('should set contructor', function() { |
||
87 | expect(someClass_instance.constructor === SomeClass).toEqual(true); |
||
88 | }); |
||
89 | }); |
||
90 | }); |
||
91 |