這個項目的目標是重構以前的解決方案來處理實際的對象。目前,當我運行茉莉花測試中,我得到這兩個錯誤:如何獲得一個構造函數值來修改其他方法?
類型錯誤:無法讀取未定義
類型錯誤的特性「分裂」:未定義
無法設置屬性「標題」爲何類不當我嘗試將其傳遞給其他方法時,識別標題值?在我嘗試將值發送到其他方法之前,它似乎工作,但現在我試圖將字符串值發送到titleCreator方法,它繼續返回undefined。
class bookTitle {
constructor(title) {
this.title = this.titleCreator(title); // this sets a title value to the bookTitle object/class
}
titleCreator(string) {
// Note that this isn't meant to be a fully fledged title creator, just designed to pass these specific tests
var littleWords = ["and", "over", "the"]; // These are the words that we don't want to capitalize
var modifiedString = this.string
.split(' ') // Splits string into array of words, basically breaks up the sentence
.map(function(word,index) {
if (index == 0) {
return capitalize(word); // capitalize the first word of the string
} else if (littleWords.indexOf(word) == -1) {
return capitalize(word); // capitalize any words that are not little, the -1 is returned by indexOf if it can't find the word in the array
} else if (littleWords.indexOf(word) >= 0) {
return word; // do not capitalize as this word is in the list of littleWords
}
})
.join(' '); // Joins every element of an array into a string with a space inbetween each value. Basically you created a sentence from an array of words
return modifiedString;
}
capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
// This function just capitalizes the word given to it
}
}
module.exports = {
bookTitle
}
編輯:這裏是我的Jasmine測試用例的上下文。該計劃的想法只是通過這些案件
var bookTitles = require ('./bookTitles.js');
describe('bookTitle', function() {
var book; // this is the object that will be passed into the test cases, returns undefined here without beforeEach
beforeEach(function() {
book = new bookTitles.bookTitle(); // creates a new book instance before each test is run
});
describe('title', function() {
it('should capitalize the first letter', function() {
book.title = 'inferno';
expect(book.title).toEqual('Inferno'); // works without capitalizing
});
it('should capitalize every word', function() {
book.title = 'stuart little';
expect(book.title).toEqual('Stuart Little');
});
describe('should capitalize every word except...', function() {
describe('articles', function() {
it('does not capitalize "the"', function() {
book.title = 'alexander the great';
expect(book.title).toEqual('Alexander the Great');
});
it('does not capitalize "a"', function() {
book.title = 'to kill a mockingbird';
expect(book.title).toEqual('To Kill a Mockingbird');
});
it('does not capitalize "an"', function() {
book.title = 'to eat an apple a day';
expect(book.title).toEqual('To Eat an Apple a Day');
});
});
it('conjunctions', function() {
book.title = 'war and peace';
expect(book.title).toEqual('War and Peace');
});
it('prepositions', function() {
book.title = 'love in the time of cholera';
expect(book.title).toEqual('Love in the Time of Cholera');
});
});
describe('should always capitalize...', function() {
it('I', function() {
book.title = 'what i wish i knew when i was 20';
expect(book.title).toEqual('What I Wish I Knew When I Was 20');
});
it('the first word', function() {
book.title = 'the man in the iron mask';
expect(book.title).toEqual('The Man in the Iron Mask');
});
});
});
});
我以前就這麼試過,而且得到了相同的錯誤代碼。我將編輯我的原始文章以包含測試用例,我對缺乏上下文感到抱歉。這個想法只是通過我寫的所有茉莉花測試案例 – mcrav95
@ mcrav95 - 還有其他幾個問題。請查看我添加到我的答案中的內容,該答案位於可運行代碼段中,以便您可以看到它創建的輸出並列出我必須做出的更改。 – jfriend00
哇謝謝你,真的超越我的意願來幫助我。我會審查答案,看看還有什麼其他的錯誤,再次感謝! – mcrav95