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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:23:24+00:00 2026-05-24T13:23:24+00:00

I am trying to sort out my RSpec tests for a controller of mine,

  • 0

I am trying to sort out my RSpec tests for a controller of mine, but its not working and I need some help figuring it out.

My Rspec is:

  before(:each) do
    @topic = mock_model(Topic, :update_attributes => true)
    Topic.stub!(:where).with({:slug=>"some-slug"}).and_return(@topic)
    with_valid_user
  end

  it "should find topic and return object" do
    Topic.should_receive(:where).with("some-slug").and_return(@topic)
    put :update, :topic_slug => "some-slug", :topic => {}
  end

The controller logic I am trying to test is:

  def get_topic
    @topic = Topic.where(:slug => params[:topic_slug]).first
    @topic
  end

But the output I am getting is:

 Failure/Error: Topic.stub!(:where).with({:slug=>"some-slug"}).first.and_return(@topic)
 NoMethodError:
   undefined method `first' for #<RSpec::Mocks::MessageExpectation:0x104c99910>
 # ./spec/controllers/topics_controller_spec.rb:41

Obviously, it seems like the method call for “first”. I understand is a bit redundant in this particular case, so I could remove it, but rather than hack around a hole in my knowledge, I’d like to learn how to do it properly (for this scenario).

Can anyone help me fill my hole?

Update:

I added the [@topic] array as suggested in the answers, but now I am getting the error:

  Failure/Error: put :update, :topic_slug => "some-slug", :topic => {}
       Mock "Topic_1001" received unexpected message :slug with (no args)

With respect to this code:

  def get_topic
    @topic = Topic.where(:slug => params[:topic_slug]).first
    @topic
  end

The parameters can’t be changed (at least not trivially). Further assistance greatly appreciated!

  • 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-24T13:23:25+00:00Added an answer on May 24, 2026 at 1:23 pm

    Change this line

    Topic.should_receive(:where).with("some-slug").and_return(@topic)

    to this

    Topic.should_receive(:where).with("some-slug").and_return([@topic])

    you expect array but return one element.

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

Sidebar

Related Questions

Just need help as I have been trying sort this out for ages now.
I'm debugging this site and trying to sort out some issues that arise in
What I'm trying to find out is whether there is some sort of equivalence
I am currently trying to sort out some logic to check an array for
I'm trying to sort out some homework regarding repetition with the while statement. The
we're trying to sort out some conventions for handling dependencies with code checked into
just trying to sort out a small delimma I'm having here. Currently, I'm working
Trying to sort out some general things considering usage of js and jquery particularly.
I've been trying to sort out some of our code using Dispose properly in
Trying to sort this out... I need a query that will combine both tables

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.