LocalJumpError: no block given

LocalJumpError: no block given

我在 运行 一个 rspec 文件时不断收到错误:

Failures:                                                                                                                                  

  1) Book title should capitalize the first letter                                                                                         
     Failure/Error: @book.title("inferno")                                                                                                 
     LocalJumpError:                                                                                                                       
       no block given (yield)                                                                                                              
     # ./08_book_titles.rb:7:in `title'  

这里是 book_title.rb

class :: Book

  attr_accessor :some_title

def title(some_title)
little_words = %w(of and for the over under an a but or nor yet so at around by after along from on to with without)
  some_title = yield
  some_title.split.each_with_index.map{|x, index| little_words.include?(x) && index > 0 ? x : x.capitalize }.join(" ")
end #def

end

这里是 rspec 文件(或者至少是第一个规范):

require_relative '08_book_titles'

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
end

我在方法中包含了收益,规范中有一个块...所以我不明白为什么它不调用该块。我已经尝试了所有我能想到的方法,从直接在地图上调用它(而不是使用分配给它的变量)到单独调用它然后在地图上使用它,再到尝试将它作为标题的参数传递在方法中......到你现在看到的。我还阅读了大量线程。我没看到什么?

我看到未来的练习也将是 运行 自定义规范 类 所以我也喜欢一些提示(或链接)关于我在测试自定义时需要知道或注意的事情类 与 rspec。谢谢!

已更新

所以我将 book_title.rb 更改为如下所示:

  class :: Book

  def title
little_words = %w(of and for the over under an a but or nor yet so at around by after along from on to with without)
    self.split.each_with_index.map{|x, index| little_words.include?(x) && index > 0 ? x : x.capitalize }.join(" ")
end #def

end

因为我可能错误地编辑了规格 - 我将它们还原为:

require '08_book_titles'

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
end

我现在得到的错误是:

Failure/Error: @book.title = "inferno"                                                                                                
     NoMethodError:                                                                                                                        
       undefined method `title=' for #<Book:0x007f292a06c6b0>                                                                              
     # ./08_book_titles_spec.rb:26:in `block (3 levels) in <top (required)>' 

您误用了 yield 不,规范中没有任何障碍。如果有,规范看起来像...

  @book.title("inferno") do
    "inferno"
  end

看到我添加的块了吗?但是你不需要一个块,你不应该使用 yield

您似乎认为您需要 yield 才能访问该参数...您不需要。您的 title 方法应该直接使用传递的参数...

def title(new_title)
little_words = %w(of and for the over under an a but or nor yet so at around by after along from on to with without)
  @some_title = new_title.split.each_with_index.map{|x, index| little_words.include?(x) && index > 0 ? x : x.capitalize }.join(" ")
end #def

最后,您的测试实际上调用了 title 方法两次,而您应该只调用一次并使用 attr_accessor 方法来测试结果。

it 'should capitalize the first letter' do
  @book.title("inferno")
  @book.some_title.should == "Inferno"
end

关于您的编辑,您不能 self.split 因为 self 是 Book 对象,而不是字符串。

您可以修改 title 方法,使其成为 setter 方法...使其成为 title= 而不是 title

  def title=(new_title)
little_words = %w(of and for the over under an a but or nor yet so at around by after along from on to with without)
    @title = new_title.split.each_with_index.map{|x, index| little_words.include?(x) && index > 0 ? x : x.capitalize }.join(" ")
end #def

这为您提供了测试显示缺少的 "title=" 方法。你可以创建一个 getter 方法...

def title
  @title
end

现在您的测试(正如您编写的那样)应该可以正常工作了。