2017-10-19 20 views
0

私はJavascriptを学び、Jasmineを使ってテストしています。私は2つのファイルを持っています。ファイルには詳細があります。この特定のセクションは機能しません。テストを実行すると、「NANが10になることを期待しています」というメッセージが表示されません。私はCounter.jsからtotalCountを取得してMyProject.jsを計算したいと思っていました。誰もこれで私を助けることができますか?1つのjavascriptファイルから別の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); 
}); 

答えて

0

私がテストを実行すると、それは「同等にNANを期待言って失敗します10 "

addIterationは、の数値がで、これにはCounterを渡しています。

project.addIteration(iteration1); 

あなたは、このような方法であなたのMyProjectのを修正することをする必要があり

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; 
} 

デモ

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());

関連する問題