0

I am following https://code.tutsplus.com/ruby-for-newbies-testing-with-rspec--net-21297t.The video creates a class

class Book

end

The test spec/book_spec.rb looks like:

require "spec_helper"

describe Book do

    before :each do
        @book = Book.new "Title","Author", :category
    end

    describe "#new" do
        it "returns a new book object" do
            @book.should be_an_instance_of Book
        end
    end
end

The test passes for the author. It fails for me. So I guess something changed in ruby? Or maybe a typo I am not able to find in my code. Can you please help?

This is my result. Thank you.

Failures:

  1) Book#new returns a new book object
     Failure/Error: @book = Book.new "Title","Author", :category
     ArgumentError:
       wrong number of arguments(3 for 0)
     # ./spec/book_spec.rb:6:in `initialize'
     # ./spec/book_spec.rb:6:in `new'
     # ./spec/book_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00058 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/book_spec.rb:11 # Book#new returns a new book object
3
  • 1
    Im checking the tutorial (net.tutsplus.com/tutorials/ruby/…) and there's nothing "wrong" with code. The tutorial is following a TDD approach so it is supposed to fail at first, then after you finish implementing the Book class , the tests should start working again. Finish the tutorial.... Commented Mar 25, 2013 at 4:26
  • Thank you. The test works for author without defining any class attributes and the initialize function. It does not work for me. From tutorial: Re-run the test (rspec spec), and you’ll find it’s passing fine. We don’t have an initialize method, so calling Ruby#new has no effect right now. Maybe I am just not reading it right. Thank you. Commented Mar 25, 2013 at 17:33
  • What you quoted refers to the part where the Book class was not yet created. If you read further the author clear states: "These will fail, so here’s the code for Book to make them pass." Then it creates the initialize and the accessors. Again, I advise you to finish the tutorial. Commented Mar 25, 2013 at 17:56

1 Answer 1

1

That’s clear that you are to define a respective constructor for Book class to call Book.new with three args.

The link above clearly says that (look at the text transcript):

# These will fail, so here’s the code for Book to make them pass:

class Book
    attr_accessor :title, :author, :category
        def initialize title, author, category
            @title = title
            @author = author
            @category = category
        end
end
Sign up to request clarification or add additional context in comments.

4 Comments

Other are running into the same problem. See two comments at the bottom of the page. I guess the tutorial can be clarified a bit. Do I still deserve a "-1"? :)
Well, I +1’ed, though everybody who claims “wrong recipe” basing on a half of ingredients deserves a tough meat instead of delicious steak :-)
okay. I confess to "not being patient" and not trying hard enough before posting :(.
“Truth is mighty and will prevail. There is nothing the matter with this, except that it ain't so.” — Mark Twain. 〈\n〉 I hope my answer was helpful and wish you all the best :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.