I'm learning Test Driven Development with Ruby and RSpec. My program should find a given word in the text. The first case should be falsey because the test_word starts with a capital and the second case should be truthy after downcasing it. When I run the spec file though, I get the
我正在使用Ruby和RSpec学习测试驱动开发。我的程序应该在文本中找到一个给定的单词。第一种情况应该是假的,因为test_word以大写字母开头,而第二种情况应该是下拉后的truthy。当我运行spec文件时,我得到了
undefined method
include?' for nil:NilClass`未定义的方法包括?'为零:NilClass`
method and the
方法和
undefined method `downcase' for nil:NilClass
nil的未定义方法`downcase':NilClass
error. How can this be resovled?
Here is my code:
错误。怎么能被重新取消?这是我的代码:
strings_spec.rb:
require_relative 'strings'
RSpec.describe BasicString do
before do
@test_word = "Courage"
@sentecne = "Success is not final, failure is not fatal: it is the courage to continue that counts!"
@text = BasicString.new(@sentence)
end
context "case-sensitive" do
it "should output interpolated text" do
result = @text.contains_word? @test_word
expect(result).to be_falsey
end
end
context "case-insensitive" do
it "should output interpolated text" do
result = @text.contains_word_ignorecase? @test_word# 'text & 'test_word' were made instance variables when 'before do' block was added.
expect(result).to be_truthy
end
end
end
strings.rb:
class BasicString
attr_reader :sentence
def initialize(sentence)#The constructor that initializes the instance variable @sentence.
@sentence = sentence
end
def contains_word?(test_word)
@sentence.include? test_word
end
def contains_word_ignorecase?(test_word)
test_word = test_word.downcase#This line downcases the test word.
@sentence.downcase.include? test_word#This test_word is downcased again for the instance variable to be sure it's downcased.
end
end
0
You have a spelling error in your before block: @sentecne
. So, you end up creating your string with a nil value since @sentence
hasn't been defined.
您之前的块中有拼写错误:@sentecne。因此,您最终会创建一个零值的字符串,因为@sentence尚未定义。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2017/11/25/49c4d326c8de7154cedbff36d9d74329.html。