这个项目的目标是重构以前的解决方案来处理实际的对象。目前,当我运行茉莉花测试中,我得到这两个错误:如何获得一个构造函数值来修改其他方法?
类型错误:无法读取未定义
类型错误的特性“分裂”:未定义
无法设置属性“标题”为何类不当我尝试将其传递给其他方法时,识别标题值?在我尝试将值发送到其他方法之前,它似乎工作,但现在我试图将字符串值发送到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