我目前正在进行TestFirst.org的Ruby练习。它是一个通过使您构建代码来传递测试来教授Ruby的程序。我提到这个说我没有写这个RSpec代码,但是很想知道如何修复它。
RSpec的:
# Book Titles in English obey some strange capitalization rules. For example,
# "and" is lowercase in "War and Peace". This test attempts to make sense of
# some of those rules.
require 'book'
describe "Book" do
before do
@book = Book.new
end
describe 'title' do
it 'should capitalize the first letter' do
@book.title = "inferno"
@book.title.should == "Inferno"
end
it 'should capitalize every word' do
@book.title = "stuart little"
@book.title.should == "Stuart Little"
end
describe 'should capitalize every word except...' do
describe 'articles' do
specify 'the' do
@book.title = "alexander the great"
@book.title.should == "Alexander the Great"
end
specify 'a' do
@book.title = "to kill a mockingbird"
@book.title.should == "To Kill a Mockingbird"
end
specify 'an' do
@book.title = "to eat an apple a day"
@book.title.should == "To Eat an Apple a Day"
end
end
specify 'conjunctions' do
@book.title = "war and peace"
@book.title.should == "War and Peace"
end
specify 'prepositions' do
@book.title = "love in the time of cholera"
@book.title.should == "Love in the Time of Cholera"
end
end
describe 'should always capitalize...' do
specify 'I' do
@book.title = "what i wish i knew when i was 20"
@book.title.should == "What I Wish I Knew When I Was 20"
end
specify 'the first word' do
@book.title = "the man in the iron mask"
@book.title.should == "The Man in the Iron Mask"
end
end
end
end
在它分析我为其编写的代码进行测试之前,它会给出这个错误:
C:\Users\Computer\Documents\learn_ruby\08_book_titles>rake
(in C:/Users/Computer/Documents/learn_ruby)
You must use ANSICON 1.31 or later (http://adoxa.3eeweb.com/ansicon/) to use colour on Windows
Book
title
should capitalize the first letter (FAILED - 1)
Failures:
1) Book title should capitalize the first letter
Failure/Error: @book = Book.new
NameError:
uninitialized constant Book
# ./08_book_titles/book_titles_spec.rb:20:in `block (2 levels) in <top (required)>'
Finished in 0 seconds
1 example, 1 failure
Failed examples:
rspec ./08_book_titles/book_titles_spec.rb:24 # Book title should capitalize the first letter
C:/RailsInstaller/Ruby2.1.0/bin/ruby.exe -S rspec C:/Users/Computer/Documents/learn_ruby/08_book_titles/book_titles_spec
.rb -IC:/Users/Computer/Documents/learn_ruby/08_book_titles -IC:/Users/Computer/Documents/learn_ruby/08_book_titles/solu
tion -f documentation -r ./rspec_config failed
我已尝试使用Google搜索错误消息,但没有运气。我刚刚开始学习,而且我没有专业知识来解决这个RSpec代码问题。对于这名有需要的学生,我们将非常感谢您的帮助。
编辑:
我是一个白痴,并不了解被问到我的是什么。继续吧。
答案 0 :(得分:1)
您需要创建一个名为Book的新类。
您可以在名为book.rb
的同一目录中创建新文件,或在测试套件的顶部添加以下行。
class Book
end
答案 1 :(得分:0)
您应该已经包含了book.rb文件。您正在寻找的解决方案是:
正如您在book_titles_spec.rb中看到的那样,有一行代码:
@book = Book.new 所以这意味着你需要创建一个名为Book
的类 课程书 端然后在第一个测试中,它调用没有参数的方法标题,所以这意味着您需要创建一个名为 title 的attr_accessor,这样您就可以将它用作实例变量。在标题方法中使用
attr_accessor:title
现在只是传递测试的逻辑,所以你可以创建一个title方法并使用@title变量来做魔术
def title @ title.split('') #所有通过测试的逻辑 @你的答案 端
我认为这对Ruby中的初学者来说是一个艰难的考验,所以这个答案可以帮助他们开始解决这个问题。