RSpec 学生练习失败
RSpec is failing for a student exercise
我目前正在做 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_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
我尝试用谷歌搜索错误消息,但没有成功。我才刚刚开始学习,我不具备解决此 RSpec 代码问题的专业知识。对于有需要的学生,我们将不胜感激。
编辑:
我是个白痴,不明白他们问我什么。那就继续吧。
您需要创建一个新的 Class 名为 Book。
您可以在名为 book.rb
的同一目录中创建一个新文件,或者在测试套件的顶部添加以下行。
class Book
end
您应该包含 book.rb 文件。您正在寻找的解决方案是:
正如你在book_titles_spec.rb中看到的,有一行代码:
@book = Book.new
所以这意味着您需要创建一个名为 Book
的 class
class 本书
结束
然后在第一个测试中,它调用了没有参数的方法 title,所以这意味着你需要创建一个名为 title 的 attr_accessor 所以您可以将它用作您将在标题方法中使用的实例变量
attr_accessor:标题
现在只是通过测试的逻辑,因此您可以创建一个标题方法并使用@title 变量来完成魔术
定义标题
@title.split(' ')
#通过测试的所有逻辑
@你的答案
结束
我认为这对Ruby的初学者来说是一个困难的测试,所以这个答案可以帮助他们开始解决这个问题。
我目前正在做 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_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
我尝试用谷歌搜索错误消息,但没有成功。我才刚刚开始学习,我不具备解决此 RSpec 代码问题的专业知识。对于有需要的学生,我们将不胜感激。
编辑:
我是个白痴,不明白他们问我什么。那就继续吧。
您需要创建一个新的 Class 名为 Book。
您可以在名为 book.rb
的同一目录中创建一个新文件,或者在测试套件的顶部添加以下行。
class Book
end
您应该包含 book.rb 文件。您正在寻找的解决方案是:
正如你在book_titles_spec.rb中看到的,有一行代码:
@book = Book.new 所以这意味着您需要创建一个名为 Book
的 classclass 本书 结束
然后在第一个测试中,它调用了没有参数的方法 title,所以这意味着你需要创建一个名为 title 的 attr_accessor 所以您可以将它用作您将在标题方法中使用的实例变量
attr_accessor:标题
现在只是通过测试的逻辑,因此您可以创建一个标题方法并使用@title 变量来完成魔术
定义标题 @title.split(' ') #通过测试的所有逻辑 @你的答案 结束
我认为这对Ruby的初学者来说是一个困难的测试,所以这个答案可以帮助他们开始解决这个问题。