2017-10-19 8 views
0

J'apprends le Javascript et les tests en utilisant Jasmine. J'ai 2 fichiers. Il y a plus de détails sur les fichiers. Cette section particulière ne fonctionne pas. Quand j'exécute le test, il échoue en disant "NAN attendu égal à 10". Je voulais obtenir le totalCount de Counter.js pour calculer MyProject.js, puis il est divisé par itération. Est-ce que quelqu'un peut m'aider avec ça?Comment obtenir la valeur d'une méthode prototype d'un fichier javascript à la méthode prototype d'un autre fichier javascript?

Counter.js

function Counter(){ 
this.count = 0; 
} 
Counter.prototype.startCount = function(count){ 
this.count += count; 
}; 
Counter.prototype.totalCount = function(){ 
return this.count; 
} 

MyProject.js

function MyProject() { 
this.iteration = 0; 
} 
MyProject.prototype.addIteration = function(iteration) { 
this.iteration += iteration; 
} 
MyProject.prototype.calculate = function() { 
var averageCount = Counter.prototype.totalCount(); 
return averageCount/this.iteration; 
} 

MyProject_spec.js

describe("MyProject", function() { 
var project, iteration1, iteration2, iteration3; 
beforeEach(function(){ 
project = new MyProject(); 
iteration1 = new Counter(); 
iteration1.startCount(10); 

iteration2 = new Counter(); 
iteration2.startCount(20); 

iteration3 = new Counter(); 
iteration3.startCount(10); 
}); 
it("can calculate(the average number of counting completed) for a set of iterations", function(){ 
project.addIteration(iteration1); 
expect(project.calculate()).toEqual(10); 
}); 

Répondre

0

Quand je lance le test, il ne parvient dire « NAN prévu à l'égalité 10 "

Votre problème est addIteration attend un numéro en tant qu'argument et vous passez le Counter au même.

project.addIteration(iteration1); 

Vous devez modifier votre MyProject de telle sorte que

function MyProject() 
{ 
    this.allIteration = []; 
    this.totalIterationCount = 0; 
} 
MyProject.prototype.addIteration = function(iteration) 
{ 
    this.allIteration.push(iteration); 
    this.totalIterationCount += iteration.totalCount(); 
} 
MyProject.prototype.calculate = function() 
{ 
    return this.totalIterationCount /this.allIteration.length; 
} 

Démo

function Counter() { 
 
    this.count = 0; 
 
} 
 
Counter.prototype.startCount = function(count) { 
 
    this.count += count; 
 
}; 
 
Counter.prototype.totalCount = function() { 
 
    return this.count; 
 
} 
 

 
function MyProject() { 
 
    this.allIteration = []; 
 
    this.totalIterationCount = 0; 
 
} 
 
MyProject.prototype.addIteration = function(iteration) { 
 
    this.allIteration.push(iteration); 
 
    this.totalIterationCount += iteration.totalCount(); 
 
} 
 
MyProject.prototype.calculate = function() { 
 
    return this.totalIterationCount/this.allIteration.length; 
 
} 
 

 
project = new MyProject(); 
 
iteration1 = new Counter(); 
 
iteration1.startCount(10); 
 
iteration2 = new Counter(); 
 
iteration2.startCount(20); 
 
iteration3 = new Counter(); 
 
iteration3.startCount(10); 
 
project.addIteration(iteration1); 
 
console.log(project.calculate()); 
 
project.addIteration(iteration2); 
 
console.log(project.calculate()); 
 
project.addIteration(iteration3); 
 
console.log(project.calculate());