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 8406447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:06:46+00:00 2026-06-09T23:06:46+00:00

Starting my journey on TDD with Rspec and having some issues. Can’t understand why

  • 0

Starting my journey on TDD with Rspec and having some issues.
Can’t understand why my rspec fails on if … OR condition.

  def director
    @movie = Movie.find_by_id(params[:id])
    if @movie.nil? or @movie.director.empty?
      flash[:notice] = "The movie has no director info"
      redirect_to movies_path
    else
      # success
    end
  end  

Rspec test:

before ... #stubs
it 'assigns movies array in director action' do
    Movie.stub(:find_by_id).and_return(@movie)
    get "director", :id => 1
    ... 
end

Error:

  1) MoviesController assigns movies array in director action
     Failure/Error: get "director", :id => 1
     NoMethodError:
       You have a nil object when you didn't expect it!
       You might have expected an instance of Array.
       The error occurred while evaluating nil.empty?

If record is not found, then @movie.nil? should satisfy the condition and @movie.director.empty? shouldn’t evaluated against; is that correct?

In this case I don’t care if there’s no such record or the director field of requested record is blank, I treat it the same way.

Appreciate your help.


UPDATE

I’ve removed some controller stuff into model like this:

controller:

  def director
    @movies = Movie.same_director_by_id(params[:id])
    if @movies.blank?
      redirect_to movies_path
    end
  end

movie:

  def self.same_director_by_id (id)
    movie = self.find_by_id(id)
    if movie.nil? or movie.director.blank?
      return []
    else
      return self.where("id != ?", id).find_all_by_director(movie.director)
    end
  end

rspec:

it 'assigns movies array in director action' do
    Movie.should_receive(:same_director_by_id).and_return([@movie])
    get "director", :id => 1
    assigns(:movies).should be_kind_of(Array)
end

Now all controller specs pass beautifully.
I’ll test model separately.

UPDATE 2:

The issue was that I stubbed :director on a Model, not on a mock!
apneadiving was correct.

  • 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-09T23:06:47+00:00Added an answer on June 9, 2026 at 11:06 pm

    As discussed in comments, it’s mandatory to mock/stub every object/method. directory was missing here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting a new GWT application and wondering if I can get some advice from
Starting to get my head around makefiles for my C programs, but having some
Starting with a list of objects containing two parameters notional and currency, how can
I'm just starting my journey into OOP - and I'm currently trying to roll
i'm new to development and i'm just starting my journey into the daunting world
Starting recently, some of my new web pages (XHTML 1.1) are setup to do
Starting with 2 png files on disk (which I can load to 2 pixmaps)
Starting with this code: new Person(ET).WithAge(88) How can it be refactored to: new Person(ET,
Starting from the solution provided by the Nick Caver I wish to save some
Starting to learn and understand phing. I would like to export sql database using

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.