Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:39:58+00:00 2026-06-11T15:39:58+00:00

I am following Rspec testing tutorial on Net.Tutsplus.com. I’ve found problem I couldn’t solve.

  • 0

I am following Rspec testing tutorial on Net.Tutsplus.com.
I’ve found problem I couldn’t solve. Here the thing.

When I run test:

C:\projekt>rspec spec/library_spec.rb –format nested

I get:

C:/projekt/spec/library_spec.rb:35:in `block (3 levels) in <top (required)>': un
defined method `books' for nil:NilClass (NoMethodError)

library_spec.rb looks like that:

require "spec_helper"

    describe "Library Object" do

    before :all do
        lib_arr = [ 
        Book.new("JavaScript: The Good Parts", "Douglas Crockford", :development),
        Book.new("Designing with Web Standarts", "Jeffrey Zeldman", :design),
        Book.new("Don't Make me Think", "Steve Krug", :usability),
        Book.new("JavaScript Patterns", "Stoyan Sefanov", :development),
        Book.new("Responsive Web Design", "Ethan Marcotte", :design)
    ]

    File.open "books.yml", "w" do |f|
        f.write YAML::dump lib_arr
    end

end

before :each do
    @lib = Library.new "books.yml"

end

describe "#new" do
    context "with no parameters" do
        it "has no books" do
            lib = Library.new
            lib.books.length.should == 0
        end
end

    context "with a yaml file name parameters " do
        it "has five books"
        @lib.books.length.should == 5
    end
end
 end

Due to tutorial instructions I changed library.rb to:

require 'yaml'

 class Library
attr_accessor :books

def initalize lib_file = false
    @lib_file = lib_file
    @books = @lib_file ? YAML::load(File.read(@lib_file)) : []
    end
 end

According to tutorial it should solve “books-NoMethodError” problem but it still apper.
Where is the problem?

Thanks for help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:39:59+00:00Added an answer on June 11, 2026 at 3:39 pm

    undefined method books for nil:NilClass (NoMethodError) just means that you are calling a method books on something that is nil, in this case @lib.

    You need to place the before(:each) hook that defines @lib inside a context or describe block, in your code it is not available in the describe '#new' block.

    Also, you were missing a do after defining the it "has five books" spec.

    I’ve corrected these errors below:

    describe "#new" do
      before :each do
        @lib = Library.new "books.yml"
      end
    
      context "with no parameters" do
        it "has no books" do
          lib = Library.new
          lib.books.length.should == 0
        end
      end
    
      context "with a yaml file name parameters " do
        it "has five books" do
          @lib.books.length.should == 5
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following the rails tutorial here: http://railstutorial.org/chapters/filling-in-the-layout#top When I run rspec spec/, I get
How can I match the following array with Rspec ? [#<struct Competitor html_url=https://github.com/assaf/vanity, description=Experiment
I'm following the ruby on rails tutorial: http://railstutorial.org/chapters/static-pages#top I'm up to using rspec. Having
I am using rspec to do testing and it is giving me the following
I'm testing a Sinatra application, which is using DataMapper, with RSpec. The following code:
I´m testing my rails app using rspec and remarkable. I have the following spec
Ive been following this introductory to Rails testing and Ive run into an issue
I am having problem with rspec testing controller the devise authentication. I have a
I'm testing my Rails application with RSpec, but then I ran into a problem.
I have the following rspec: require 'spec_helper' describe KnowledgesController do before (:each) do @knowledge

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.