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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:31:41+00:00 2026-05-15T13:31:41+00:00

I am trying to specify in my RSpec tests that my controller should use

  • 0

I am trying to specify in my RSpec tests that my controller should use current_user.projects.find() instead of Project.find() I am using the Mocha mocking framework and was trying something like this:

controller.current_user.projects.expects(:find).returns(@project)

I have already mocked out controller.stubs(:current_user).returns(@profile)

This test passes with this even when I use the Project.find() implementation. How can I test that my controller is calling off of the correct object?

Edit (adding additional code):

I have Projects and Tasks, Project have many tasks. This is the show method for displaying a task in a project that is owned by current_user

Action in the controller:

def show
    @project = current_user.projects.find_by_id(params[:cardset_id])

    if @project.nil?
      flash[:notice] = "That project doesn't exist. Try again."
      redirect_to(projects_path)
    else
      @task = @project.tasks.find_by_id(params[:id])
    end
  end

This is the test that is not checking that the cardsets method was called off the current_user object.

Current Test:

context "with get to show" do
  context "with valid project" do
    before(:each) do
      @project = Factory(:project)
      @task = Factory(:task)
      @profile = @project.profile
      ApplicationController.stubs(:require_user).returns(true)
      controller.stubs(:current_user).returns(@profile)

      Project.stubs(:find_by_id).returns(@project)
      @project.tasks.stubs(:find_by_id).returns(@task)
      get :show, :project_id => @project.id, :id => @task.id
    end

    it "should assign task" do
      assigns[:task].should_not be_nil
    end

    it "should assign project" do
      assigns[:project].should_not be_nil
    end
  end

  context "with invalid project" do
    before(:each) do
      Project.stubs(:find_by_id).returns(nil)
      get :show, :project_id => @project.id, :id => @task.id
    end

    it "should set flash" do
      flash[:notice].should match(/doesn't exist/i)
    end

    it "should redirect" do
      response.should redirect_to(cardsets_url)
    end
  end
end
  • 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-15T13:31:41+00:00Added an answer on May 15, 2026 at 1:31 pm

    Based on the little you’ve told us, I think you need:

    @profile.expects(:find).returns(@project)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write some XML schema code to specify that a particular
I am trying to put together an index using terms, which I specify as
I'm trying to use this env variable to specify the path for my templates
tl;dr: Jump to the last paragraph Recently I've been trying to use RSpec's request
I'm trying to specify the select-before-update attribute in a ClassMap using Fluent NHibernate, but
I'm trying to specify a new packet format using scapy . In the packet
In my Java application, I'm trying to specify a Pattern that would match anything
I'm trying to specify the licence for my wix setup project. I have created
I am trying to write up a few tests using JMock and the following
I'm trying to specify a route configuration that will allow a url similar to:

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.