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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:32:32+00:00 2026-06-03T15:32:32+00:00

I am using Ruby on Rails 3.2.2, Rspec 2.9.0 and RspecRails 2.9.0. I am

  • 0

I am using Ruby on Rails 3.2.2, Rspec 2.9.0 and RspecRails 2.9.0. I am trying to test a new controller action and I would like to know why I get the error explained above only for that action.

Given:

# controller
class ArticlesController < ApplicationController
  before_filter :signed_in

  def new
    @article = Article.new

    # This is just a sample code line to show you where the error happens?
    @article.new_record?

    ...
  end

  def show
    @article = Article.find(params[:id])

    ...
  end
end

# spec file
require 'spec_helper'

describe ArticlesController do
  before(:each) do
    @current_user = FactoryGirl.create(:user)

    # Signs in user so to pass the 'before_filter'
    cookies.signed[:current_user_id] = {:value => [@current_user.id, ...]}
  end

  it "article should be new" do
    article = Article.should_receive(:new).and_return(Article.new)
    get :new
    assigns[:article].should eq(article)
  end

  it "article should be shown" do
    article = FactoryGirl.create(:article)

    get :show, :id => article.id.to_s

    assigns[:article].should eq(article)
  end
end

When I run the Example related to the new action I get this error (it is related to the @article.new_record? code line in the controller file):

Failure/Error: get :new
NoMethodError:
  undefined method `new_record?' for nil:NilClass

But when I run the Example related to the show action it passes without errors.

What is the problem? How can I solve that?

  • 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-03T15:32:33+00:00Added an answer on June 3, 2026 at 3:32 pm

    The problem is the way you’ve done

    Article.should_receive(:new).and_return(Article.new)
    

    This is the same as

    temp = Article.should_receive(:new)
    temp.and_return(Article.new)
    

    So by the time you are setting up the return value, Article.new has already been mocked out and so returns nil, so you’re doing and_return(nil) Create the return value first, i.e.

    new_article = Article.new #or any other way of creating an article - it may also be appropriate to return a mock
    Article.should_receive(:new).and_return(new_article)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ruby on Rails 3.2.2 and rspec-rails-2.8.1. I would like to output
I am using Ruby on Rails 3.2.2, Rspec 2.9.0 and RspecRails 2.9.0. I would
I am using Ruby on Rails 3.0.9, RSpec-rails 2 and FactoryGirl. I would like
I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. I would like
Using ruby(1.8.7) , rails(2.3.8) , rspec(1.3.1) and rspec-rails(1.3.3) how do I test a method
I am using cucumber and RSpec for my new ruby on rails application and
I am using Ruby on Rails 3.0.9, RSpec-rails 2 and FactoryGirl. I am trying
I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would
I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would
I am using Ruby on Rails 3.0.9 and RSpec 2. I know that there

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.