I keep getting an error while running a rspec file:
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'
here is the 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
and here is the rspec file (or at least the first spec):
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
I have included the yield in the method, and there IS a block in the spec...so I don't understand why its not calling that block. I have tried everything I could think of from calling it directly on the map (instead of using a variable it is assigned to) to calling it separately on its own THEN using it on map, to trying to pass it in as the argument of title in the method...to what you see now. I have also read through a ton of threads. What am I not seeing?
I see that future exercises will also be running specs on custom classes so I would also love some tips (or links) on things I need to know or look out for while testing custom classes with rspec. Thanks!
UPDATED
So i have changed my book_title.rb to look like this:
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
and because I, probably incorrectly, edited the specs- I reverted them back to this:
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
and the error I now get is this:
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)>'
You're mis-using yield and no, there's no block in the spec. If there was, the spec would look like...
@book.title("inferno") do
"inferno"
end
See the block I added? But you don't need a block and you shouldn't use yield
You seem to be thinking that you need yield to access the argument... you don't. Your title method should just use the passed argument directly...
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
Finally, your test is actually calling the title method twice, when you should only call it once and use the attr_accessor method to test the result.
it 'should capitalize the first letter' do
@book.title("inferno")
@book.some_title.should == "Inferno"
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With