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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:28:38+00:00 2026-05-27T16:28:38+00:00

How can I describe of finding an associated model in the RSpec-rails controller examples?

  • 0

How can I describe of finding an associated model in the RSpec-rails controller examples?

I try the following:

Article

class Article < ActiveRecord::Base
  has_many :comments
end

Comment

class Comment < ActiveRecord::Base
end

CommentsControllerSpec

describe CommentsController do
  describe 'update' do
    let(:article) { stub_model Article }
    let(:comment) { stub_model Comment }

    before { Article.stub(:find) { article } }
    before { article.stub(:comments) { [comment] } }

    it 'finds a comment' do
      article.comments.should_receive(:find) { comment }
      put :update, article_id: article.id, id: comment.id
    end
  end
end

CommentsController

class CommentsController < ApplicationController
  def update
    Article.find.comments.find
  end
end

But unfortunatelly it doesn’t work. Any ideas?

Thanks.

  • 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-05-27T16:28:39+00:00Added an answer on May 27, 2026 at 4:28 pm

    I tackled the problem by myself.

    Here’s the approximate list of actions to describe of finding an associated model in the Rails’ controllers.

    1.

    describe CommentsController do
      describe 'update' do
      end
    end
    

    2.

    describe CommentsController do
      describe 'update' do
        it 'finds an article' # <=
      end
    end
    

    3.

    describe CommentsController do
      describe 'update' do
        it 'finds an article' do
          Article.should_receive(:find) # <=
          put :update                   # <=
        end
      end
    end
    

    4.

    No route matches {:controller=>"comments", :action=>"update"}
    

    5.

    describe CommentsController do
      describe 'update' do
        let(:article) { stub_model Article } # <=
    
        it 'finds a article' do
          Article.should_receive(:find)
          put :update, article_id: article.id # <=
        end
      end
    end
    

    6.

    No route matches \
      {:article_id=>"1025", :controller=>"comments", :action=>"update"}
    

    7.

    describe CommentsController do
      describe 'update' do
        let(:article) { stub_model Article }
        let(:comment) { stub_model Comment } # <=
    
        it 'finds an article' do
          Article.should_receive(:find)
          put :update, article_id: article.id, id: comment.id # <=
        end
      end
    end
    

    8.

    (<Article (class)>).find(any args)
           expected: 1 time
           received: 0 times
    

    9.

    class CommentsController < ApplicationController
      def update
        Article.find
      end
    end
    

    10.

    Success!
    

    11.

    describe CommentsController do
      describe 'update' do
        let(:article) { stub_model Article }
        let(:comment) { stub_model Comment }
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s) # <=
          put :update, article_id: article.id, id: comment.id
        end
      end
    end
    

    12.

    <Article (class)> received :find with unexpected arguments
      expected: ("1025")
           got: (no args)
    

    13.

    class CommentsController < ApplicationController
      def update
        Article.find(params[:article_id]) # <=
      end
    end
    

    14.

    Success!
    

    15.

    describe CommentsController do
      describe 'update' do
        let(:article)     { stub_model Article }
        let(:comment)     { stub_model Comment }
        let(:association) {                                    # <=
          mock(ActiveRecord::Associations::HasManyAssociation) # <=
        }                                                      # <=
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s)
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'finds a comment' do                               # <=
          article.stub(:comments) { association }             # <=
          association.should_receive(:find)                   # <=
          put :update, article_id: article.id, id: comment.id # <=
        end                                                   # <=
      end
    end
    

    16.

    Couldn't find Article with id=1027
    

    17.

    describe CommentsController do
      describe 'update' do
        let(:article)     { stub_model Article }
        let(:comment)     { stub_model Comment }
        let(:association) {
          mock(ActiveRecord::Associations::HasManyAssociation)
        }
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s)
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'finds a comment' do
          Article.stub(:find) { article } # <=
          article.stub(:comments) { association }
          association.should_receive(:find)
          put :update, article_id: article.id, id: comment.id
        end
      end
    end
    

    18.

    (Mock ActiveRecord::Associations::HasManyAssociation).find(any args)
        expected: 1 time
        received: 0 times
    

    19.

    class CommentsController < ApplicationController
      def update
        Article.find(params[:article_id]).comments.find # <=
      end
    end
    

    20.

    update
      finds an article (fail!)
      finds a comment  (success!)
    
    undefined method `comments' for nil:NilClass
    

    21.

    describe CommentsController do
      describe 'update' do
        let(:article)     { stub_model Article }
        let(:comment)     { stub_model Comment }
        let(:association) {
          mock(ActiveRecord::Associations::HasManyAssociation)
        }
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s) { article } # <=
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'finds a comment' do
          Article.stub(:find) { article }
          article.stub(:comments) { association }
          association.should_receive(:find)
          put :update, article_id: article.id, id: comment.id
        end
      end
    end
    

    22.

    update
      finds an article (fail!)
      finds a comment  (success!)
    
    Couldn't find Comment without an ID
    

    23.

    describe CommentsController do
      describe 'update' do
        let(:article)     { stub_model Article }
        let(:comment)     { stub_model Comment }
        let(:association) {
          mock(ActiveRecord::Associations::HasManyAssociation)
        }
    
        before { article.stub(:comments) { association } } # <=
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s) { article }
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'finds a comment' do
          Article.stub(:find) { article }
          # -> article.stub(:comments) { association } <-
          association.should_receive(:find)
          put :update, article_id: article.id, id: comment.id
        end
      end
    end
    

    24.

    update
      finds an article (fail!)
      finds a comment  (success!)
    
    Mock ActiveRecord::Associations::HasManyAssociation \
      received unexpected message :find with (no args)
    

    25.

    describe CommentsController do
      describe 'update' do
        let(:article)     { stub_model Article }
        let(:comment)     { stub_model Comment }
        let(:association) {
          mock(ActiveRecord::Associations::HasManyAssociation).as_null_object # <=
        }
    
        before { article.stub(:comments) { association } }
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s) { article }
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'finds a comment' do
          Article.stub(:find) { article }
          association.should_receive(:find)
          put :update, article_id: article.id, id: comment.id
        end
      end
    end
    

    26.

    Success!
    

    27.

    describe CommentsController do
      describe 'update' do
        let(:article)     { stub_model Article }
        let(:comment)     { stub_model Comment }
        let(:association) {
          mock(ActiveRecord::Associations::HasManyAssociation).as_null_object
        }
    
        before { article.stub(:comments) { association } }
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s) { article }
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'finds a comment' do
          Article.stub(:find) { article }
          association.should_receive(:find).with(comment.id.to_s) # <=
          put :update, article_id: article.id, id: comment.id
        end
      end
    end
    

    28.

    Mock ActiveRecord::Associations::HasManyAssociation \
      received :find with unexpected arguments
      expected: ("1028")
           got: (no args)
    

    29.

    class CommentsController < ApplicationController
      def update
        Article.find(params[:article_id]).comments.find(params[:id]) # <=
      end
    end
    

    30.

    Success!
    

    31.

    describe CommentsController do
      describe 'update' do
        let(:article)     { stub_model Article }
        let(:comment)     { stub_model Comment }
        let(:association) {
          double(ActiveRecord::Associations::HasManyAssociation).as_null_object # <=
        }
    
        before { article.stub(:comments) { association } }
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s) { article }
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'finds a comment' do
          Article.stub(:find) { article }
          association.should_receive(:find).with(comment.id.to_s)
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'assigns @comment' do                              # <=
          Article.stub(:find) { article }                     # <=
          association.stub(:find) { comment }                 # <=
          put :update, article_id: article.id, id: comment.id # <=
          assigns[:comment].should eq comment                 # <=
        end                                                   # <=
      end
    end
    

    32.

    expected: #<Comment >
         got: nil
    

    33.

    class CommentsController < ApplicationController
      def update
        @comment = Article.find(params[:article_id]).comments.find(params[:id]) # <=
      end
    end
    

    34.

    Success!
    

    35.

    describe CommentsController do
      describe 'update' do
        let(:article)     { stub_model Article }
        let(:comment)     { stub_model Comment }
        let(:association) {
          double(ActiveRecord::Associations::HasManyAssociation).as_null_object
        }
    
        before { article.stub(:comments) { association } }
    
        it 'finds an article' do
          Article.should_receive(:find).with(article.id.to_s) { article }
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'finds a comment' do
          Article.stub(:find) { article }
          association.should_receive(:find).with(comment.id.to_s)
          put :update, article_id: article.id, id: comment.id
        end
    
        it 'assigns @article' do                              # <=
          Article.stub(:find) { article }                     # <=
          put :update, article_id: article.id, id: comment.id # <=
          assigns[:article].should eq article                 # <=
        end                                                   # <=
    
        it 'assigns @comment' do
          Article.stub(:find) { article }
          association.stub(:find) { comment }
          put :update, article_id: article.id, id: comment.id
          assigns[:comment].should eq comment
        end
      end
    end
    

    36.

    expected: #<Article >
         got: nil
    

    37.

    class CommentsController < ApplicationController
      def update
        @article = Article.find(params[:article_id])   # <=
        @comment = @article.comments.find(params[:id]) # <=
      end
    end
    

    38.

    Success!
    

    39.

    class CommentsController < ApplicationController
      before_filter :find_article, only: :update # <=
      before_filter :find_comment, only: :update # <=
    
      def update
        # -> @article = Article.find(params[:article_id])   <-
        # -> @comment = @article.comments.find(params[:id]) <-
      end
    
      private                                          # <=
        def find_article                               # <=
          @article = Article.find(params[:article_id]) # <=
        end                                            # <=
    
        def find_comment                                 # <=
          @comment = @article.comments.find(params[:id]) # <=
        end                                              # <=
    end
    

    40.

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

Sidebar

Related Questions

Can anyone describe the following php function: function get_setting_value($settings_array, $setting_name, $default_value = ) {
How can I describe RSpec test to check if the array doesn't include some
Okay I'll try describe this the best way I can. I have a chat
I am using the following InnoDB tables in mysql to describe records that can
Can anyone describe the difference in behavior between BOOST_CHECK_CLOSE and BOOST_CHECK_CLOSE_FRACTION ? The documentation
How can you describe the parameters and return type (getter/setter) of your PHP functions?
Why there is InvalidCastException thrown? Can someone describe me this behavior? object zero =
I've got a problem concerning combinatorics. Unfortunately, I can't describe it abstractly so I
I can't best describe this in words, so I'll show you with pictures. Here's
can anyone point to a site or describe how I can go about designing

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.